在iPhone中创建高效的缩略图

时间:2010-06-12 19:43:08

标签: iphone uiimage thumbnails

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

3 个答案:

答案 0 :(得分:8)

从图像中快速创建缩略图的两种方法的比较 请详细了解此链接 http://www.cocoaintheshell.com/2011/01/uiimage-scaling-imageio/ 要么 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
  • 实时字节: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
  • 实时字节:681Kb
  • 使用的总字节数:77.63Mb

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

答案 1 :(得分:1)

答案 2 :(得分:-3)

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

但是,远非效率。