What's the most efficient way to create a thumbnail from an arbitrary web image in iPhone?
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 :
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 :
You can see that using ImageIO is about 19% faster than UIKit, and also uses slightly less memory.
This is best way that I've found.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With