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).
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; }
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