首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIImage initWithContentsOfFile:... -只需获取尺寸

UIImage initWithContentsOfFile:... -只需获取尺寸
EN

Stack Overflow用户
提问于 2012-04-06 22:52:53
回答 1查看 97关注 0票数 0

我只需要从PNG文件中获取宽度和高度。有没有可能使用UIImage?原因是有时(在极少数情况下,某些图像具有奇数宽度和RGB8格式) UIImage返回的尺寸与libpng不同)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-11 02:19:15

我在这里找到了答案:http://oleb.net/blog/2011/09/accessing-image-properties-without-loading-the-image-into-memory/

代码语言:javascript
复制
#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);
}

我们可以从文件中访问任何元数据,而无需加载图像。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10045185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档