0
我只需要從PNG文件中獲取寬度和高度。是否可以使用UIImage
?原因是因爲有時(極少數情況下,某些圖像具有奇數寬度和RGB8格式)UIImage返回的尺寸不同於libpng
)。UIImage initWithContentsOfFile:... - 只需要尺寸
我只需要從PNG文件中獲取寬度和高度。是否可以使用UIImage
?原因是因爲有時(極少數情況下,某些圖像具有奇數寬度和RGB8格式)UIImage返回的尺寸不同於libpng
)。UIImage initWithContentsOfFile:... - 只需要尺寸
我已經找到了答案在這裏: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);
}
我們可以從文件訪問的任何元數據,而無需加載圖像。