我正在开发一个应用程序,用户可以在其中尝试加载非常非常大的图像。这些图像首先在表格视图中显示为缩略图。我的原始代码会在大图像上崩溃,因此我重写它以首先将图像直接下载到磁盘。
是否有一种已知的方法可以调整磁盘上图像的大小,而无需通过以下方式将其完全加载到内存中UIImage
?我目前正在尝试使用类别来调整大小UIImage
详细说明here http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/,但我的应用程序在尝试缩略图非常大的图像时崩溃(例如,this http://imgsrc.hubblesite.org/hu/db/images/hs-2003-28-a-full_jpg.jpg- 警告,巨大的图像)。
您应该查看 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)