我只需要从PNG文件中获取宽度和高度。有没有可能使用UIImage?原因是有时(在极少数情况下,某些图像具有奇数宽度和RGB8格式) UIImage返回的尺寸与libpng不同)。
发布于 2012-07-11 02:19:15
我在这里找到了答案:http://oleb.net/blog/2011/09/accessing-image-properties-without-loading-the-image-into-memory/
#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);
}我们可以从文件中访问任何元数据,而无需加载图像。
https://stackoverflow.com/questions/10045185
复制相似问题