gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:23:38 27 4
gpt4 key购买 nike

假设我想找出图像的大小,因此如果用户试图在我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com