iPhone 中高效的缩略图创建

发布于 2024-09-05 14:46:24 字数 39 浏览 5 评论 0原文

在 iPhone 中从任意网页图像创建缩略图的最有效方法是什么?

What's the most efficient way to create a thumbnail from an arbitrary web image in iPhone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幻想少年梦 2024-09-12 14:46:24

两种快速创建图像缩略图方法的比较
请参阅此链接了解详细信息
http://www.cocoaintheshell.com/2011/01/uiimage-scaling-图像/
或者
http://vocaro.com/ trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

对于未来,只需从第一个复制粘贴代码

第一种方法,使用 UIKit

void)buildGallery
{
    for (NSUInteger i = 0; i < kMaxPictures; i++)
    {
        NSInteger imgTag = i + 1;
        NYXPictureView* v = [[NYXPictureView alloc] initWithFrame:(CGRect){.origin.x = x, .origin.y = y, .size = _thumbSize}];
        NSString* imgPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d", imgTag] ofType:@"jpg"];
        UIImage* fullImage = [[UIImage alloc] initWithContentsOfFile:imgPath];
        [v setImage:[fullImage imageScaledToFitSize:_thumbSize]];
        [fullImage release];
}

结果长凳给了我以下内容:

  • Time Profiler:4233ms
  • Live bytes:695Kb
  • 使用的总字节数:78.96Mb

第二种方法,使用 ImageIO

-(void)buildGallery
{
    for (NSUInteger i = 0; i < kMaxPictures; i++)
    {
        NSInteger imgTag = i + 1;
        NYXPictureView* v = [[NYXPictureView alloc] initWithFrame:(CGRect){.origin.x = x, .origin.y = y, .size = _thumbSize}];
        NSString* imgPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d", imgTag] ofType:@"jpg"];
        CGImageSourceRef src = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:imgPath], NULL);
        CFDictionaryRef options = (CFDictionaryRef)[[NSDictionary alloc] initWithObjectsAndKeys:(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform, (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent, (id)[NSNumber numberWithDouble:_maxSize], (id)kCGImageSourceThumbnailMaxPixelSize, nil];
        CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options); // Create scaled image
        CFRelease(options);
        CFRelease(src);
        UIImage* img = [[UIImage alloc] initWithCGImage:thumbnail];
        [v setImage:img];
        [img release];
        CGImageRelease(thumbnail);
    }

他长凳给了我这个:

  • Time Profiler:3433ms
  • Live bytes:681Kb
  • 使用的总字节数: 77.63Mb

您可以看到使用 ImageIO 比 UIKit 快约 19%,并且使用的内存也略少

the comparison of two method for fast creating thumbnail from image
kindly see this link for detail
http://www.cocoaintheshell.com/2011/01/uiimage-scaling-imageio/
or
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

for future , just copy pasting code from first one

First method, using UIKit

void)buildGallery
{
    for (NSUInteger i = 0; i < kMaxPictures; i++)
    {
        NSInteger imgTag = i + 1;
        NYXPictureView* v = [[NYXPictureView alloc] initWithFrame:(CGRect){.origin.x = x, .origin.y = y, .size = _thumbSize}];
        NSString* imgPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d", imgTag] ofType:@"jpg"];
        UIImage* fullImage = [[UIImage alloc] initWithContentsOfFile:imgPath];
        [v setImage:[fullImage imageScaledToFitSize:_thumbSize]];
        [fullImage release];
}

Results of the benches gave me the following :

  • Time Profiler : 4233ms
  • Live bytes : 695Kb
  • Overall bytes used : 78.96Mb

Second method, using ImageIO

-(void)buildGallery
{
    for (NSUInteger i = 0; i < kMaxPictures; i++)
    {
        NSInteger imgTag = i + 1;
        NYXPictureView* v = [[NYXPictureView alloc] initWithFrame:(CGRect){.origin.x = x, .origin.y = y, .size = _thumbSize}];
        NSString* imgPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d", imgTag] ofType:@"jpg"];
        CGImageSourceRef src = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:imgPath], NULL);
        CFDictionaryRef options = (CFDictionaryRef)[[NSDictionary alloc] initWithObjectsAndKeys:(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform, (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent, (id)[NSNumber numberWithDouble:_maxSize], (id)kCGImageSourceThumbnailMaxPixelSize, nil];
        CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options); // Create scaled image
        CFRelease(options);
        CFRelease(src);
        UIImage* img = [[UIImage alloc] initWithCGImage:thumbnail];
        [v setImage:img];
        [img release];
        CGImageRelease(thumbnail);
    }

he benches gave me this :

  • Time Profiler : 3433ms
  • Live bytes : 681Kb
  • Overall bytes used : 77.63Mb

You can see that using ImageIO is about 19% faster than UIKit, and also uses slightly less memory.

九局 2024-09-12 14:46:24

到目前为止,我发现适用于任何图像的唯一方法是将其显示在具有正确大小的 ImageView 中,然后从该视图创建位图。

但远非高效。

The only way that I've found so far that works on any images is to show it in an ImageView with the right size, and then create a bitmap from that view.

Far from efficient, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文