调整UIImage的大小,而不将其完全加载到内存? [英] resizing a UIImage without loading it entirely into memory?

查看:187
本文介绍了调整UIImage的大小,而不将其完全加载到内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing an application where a user could potentially try and load very very large images. These images are first displayed as thumbnails in a table view. My original code would crash on large images, so I'm rewriting it to first download the image directly to disk.

有一个已知的方法来调整磁盘上的图像大小而不是通过 UIImage 完全加载到内存中?我目前正尝试使用 UIImage 上的类别调整大小这里,但我的应用程式尝试缩图非常大的图像时崩溃(例如, - 警告,巨大的形象)。

Is there a known way to resize an image on disk without entirely loading it into memory via UIImage? I'm currently trying to resize using the categories on UIImage as detailed here, but my app crashes on attempting to thumbnail a very large image (like, for example, this - warning, huge image).

推荐答案

您应该看看ImageIO.framework中的CGImageSource,但它只能从iOS 4.0开始使用。

You should take a look at CGImageSource in ImageIO.framework, but it is only available since iOS 4.0.

p>

Quick example :

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

这篇关于调整UIImage的大小,而不将其完全加载到内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆