Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resizing a UIImage without loading it entirely into memory?

Tags:

I'm developing an application where a user could potentially try and load very very large images. These images are first displayed as thumbnails in a table view. My original code would crash on large images, so I'm rewriting it to first download the image directly to disk.

Is there a known way to resize an image on disk without entirely loading it into memory via UIImage? I'm currently trying to resize using the categories on UIImage as detailed here, but my app crashes on attempting to thumbnail a very large image (like, for example, this - warning, huge image).

like image 604
kevboh Avatar asked May 02 '11 17:05

kevboh


1 Answers

You should take a look at CGImageSource in ImageIO.framework, but it is only available since iOS 4.0.

Quick example :

-(UIImage*)resizeImageToMaxSize:(CGFloat)max path:(NSString*)path {     CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], NULL);     if (!imageSource)         return nil;      CFDictionaryRef options = (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:         (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,         (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,         (id)@(max),         (id)kCGImageSourceThumbnailMaxPixelSize,         nil];     CGImageRef imgRef = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options);      UIImage* scaled = [UIImage imageWithCGImage:imgRef];      CGImageRelease(imgRef);     CFRelease(imageSource);      return scaled; } 
like image 100
Nyx0uf Avatar answered Nov 09 '22 19:11

Nyx0uf