调整UIImage的大小,而不是完全加载到内存?

我正在开发一个应用程序,用户可能会尝试加载非常大的图像。 这些图像首先显示为表格视图中的缩略图。 我原来的代码会在大图像上崩溃,所以我重写它首先直接将图像下载到磁盘。

有没有一种已知的方式来调整磁盘上的图像,而不是通过UIImage将其载入内存? 我目前正在尝试使用UIImage上的类别来resize,但是我的应用程序在试图缩小非常大的图像(例如, 这个警告,巨大的图像)时崩溃了。

您应该在ImageIO.framework中查看CGImageSource,但是它只能在iOS 4.0以上使用。

快速示例:

 -(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; }