objective-c - 调整 UIImage 的大小而不将其完全加载到内存中?

标签 objective-c cocoa-touch ios uiimage

我正在开发一个应用程序,用户可以在其中尝试加载非常非常大的图像。这些图像首先在表格 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/

相关文章:

iPhone:对于更快的蓝牙最有用的技术是什么?

ios - UITableView 清空 iOS 9 beta 5 更新

ios - Xcode 5 Asset Catalog 是否向后兼容 iOS 7 之前的版本?

ios - 变换使 UIView 在 iPhone 6 设备上消失

objective-c - 使用应用程序 didReceiveRemoteNotification 处理程序来获取推送通知消息

ios - 使用 setText 时未调用 UITextField drawPlaceholderInRect

cocoa-touch - 如何从短网址获取长网址

ios - 如何在 SwiftUI 中创建多行 TextField?

ios - 这个数组扩展中的 "+"闭包在做什么?

ios - NSInternalInconsistencyException,原因 : Could not load NIB in bundle