当我用大图像(即大于10 MB)测试时,以下创建缩略图图像的方法在iPad上崩溃.我对它进行了分析,Allocations没有报告任何大的内存峰值 - 它在运行期间始终保持5 MB的活动内存.
如何为图像创建如此大的缩略图?我已经尝试使用Core Graphics进行扩展,但这样会降低内存效率并且不起作用.
+(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;
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在主线程,工作线程,并发,非并发等上尝试过它 - 它似乎不适用于实际的设备.
同样奇怪的是,它的工作效果非常出色,PDF大于60 MB.