Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient thumbnail creation in iPhone

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

like image 417
hpique Avatar asked Jun 12 '10 19:06

hpique


2 Answers

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.

like image 106
Mohit Nigam Avatar answered Nov 01 '22 03:11

Mohit Nigam


This is best way that I've found.

like image 2
TechZen Avatar answered Nov 01 '22 03:11

TechZen