0

PNGファイルから幅と高さだけを取得する必要があります。で行うことは可能UIImageですか?その理由は、UIImageが)とは異なるサイズを返す場合があるためです(まれに、幅が奇数でRGB8形式の画像がある場合libpng)。

4

1 に答える 1

0

私はここで答えを見つけました: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);
}

画像を読み込まずに、ファイルから任意のメタデータにアクセスできます。

于 2012-07-10T18:19:15.280 に答える