我正在开发一个应用程序,用户可以在其中尝试加载非常非常大的图像。这些图像首先在表格 View 中显示为缩略图。我的原始代码会在大图像上崩溃,所以我重写它以首先将图像直接下载到磁盘。
有没有一种已知的方法可以调整磁盘上图像的大小,而无需通过 UIImage
将其完全加载到内存中?我目前正在尝试使用 UIImage
上的类别调整大小,详情 here ,但我的应用程序在尝试缩略一张非常大的图像时崩溃(例如,this - 警告,巨大的图像)。
最佳答案
您应该看看 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;
}
关于objective-c - 调整 UIImage 的大小而不将其完全加载到内存中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5860215/