ios - Core Image 会立即加载图像数据吗?

标签 ios core-image

假设我想找出图像的大小,因此如果用户试图在我的 iPad 应用程序中加载 10,000x10,000 像素的图像,我可以向他们显示一个对话框而不会崩溃。如果我执行 [UIImage imageNamed:][UIImage imageWithContentsOfFile:] 这将立即将我可能很大的图像加载到内存中。

如果我改用 Core Image,像这样说:

CIImage *ciImage = [CIImage imageWithContentsOfURL:[NSURL fileURLWithPath:imgPath]];

然后向我的新 CIImage 询问它的大小:

CGSize imgSize = ciImage.extent.size;

它会将整个图像加载到内存中来告诉我这一点,还是只查看文件的元数据来发现图像的大小?

最佳答案

imageWithContentsOfURL 函数将图像加载到内存中,是的。

幸运的是,Apple 实现了 CGImageSource 来读取图像元数据,而无需在 iOS4 中将实际像素数据加载到内存中,您可以阅读如何使用它 in this blog post (方便地,它提供了有关如何获取图像尺寸的代码示例)。

编辑:在此处粘贴代码示例以防止链接失效:

#import <ImageIO/ImageIO.h>

NSURL *imageFileURL = [NSURL fileURLWithPath:...];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
if (imageSource == NULL) {
    // Error loading image
    ...
    return;
}

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                     [NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache,nil];
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);
if (imageProperties) {
    NSNumber *width = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
    NSNumber *height = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
    NSLog(@"Image dimensions: %@ x %@ px", width, height);
    CFRelease(imageProperties);
}

完整的 API 引用是 also available here .

关于ios - Core Image 会立即加载图像数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11680237/

相关文章:

ios - 我可以在一个 ViewController 中有两个 UISearchBar 吗?

ios - 更改单例内容

ios - CoreImage 人脸检测发生在每一帧

iphone - 应用不同的过滤器

iphone - NavigationController 和模态视图

ios - 如何在 UIWebView 中指示网页加载进度?

c# - AVAudioRecorder 始终为空

ios - initWithCVPixelBuffer 失败,因为 CVPixelBufferRef 不是非 IOSurface 支持的

ios - float 性能成本 ↔︎ Metal 转换的一半

ios - 仅将 CIGaussianBlur 应用于图像的一小部分