CGImageSourceCreateThumbnailAtIndex crashes with 20MB Image
The following method for creating a thumbnail image crashes on an iPad
when I test it with large images (ie > 10 MB). I've profiled it and
Allocations doesn't report any large memory spikes - it stays consistently
at 5 MB of living memory during the operation.
How can I create a thumbnail for an image so large? I've tried scaling it
with Core Graphics but that is less memory efficient and doesn't work.
+(UIImage*) thumbnailImageAtPath:(NSString*) path withSize:(CGSize) size{
@autoreleasepool{
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge
CFURLRef)[NSURL fileURLWithPath:path], NULL);
if(!src){
return nil;
}
NSDictionary* options = @{
(id)kCGImageSourceShouldAllowFloat :
(id)kCFBooleanTrue,
(id)kCGImageSourceCreateThumbnailWithTransform
: (id)kCFBooleanFalse,
(id)kCGImageSourceCreateThumbnailFromImageIfAbsent
: (id)kCFBooleanTrue,
(id)kCGImageSourceThumbnailMaxPixelSize
: [NSNumber numberWithDouble:1024]
};
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0,
(__bridge CFDictionaryRef)options);
// Doesn't reach here :(
UIImage* img = [[UIImage alloc] initWithCGImage:thumbnail];
NSLog(@"Size: %f, %f", size.width, size.height);
CGImageRelease(thumbnail);
CFRelease(src);
return img;
}
}
I've tried it on the main thread, worker threads, concurrently,
non-concurrently, etc - it just doesn't seem to work on an actual device.
What's also strange is that it works brilliantly with a PDF of > 60 MB.
No comments:
Post a Comment