Frage

How can I get dimensions of an image file saved in Documents Directory?

I'm able to get other attributes like NSFileCreationDate and NSFileSize by using the following code

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:imageFile error:nil];
NSDate *date = (NSDate*)[attributes objectForKey:NSFileCreationDate];
War es hilfreich?

Lösung

As Duncan said, you can't do it this way. But yes can use ImageIO framework. Please try the code below.

NSURL *imageFileURL = [NSURL fileURLWithPath:...]; // Put your image in a URL
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
if (imageSource == NULL) {
// Error loading image
...
return;
}

CGFloat width = 0.0f, height = 0.0f;
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
if (imageProperties != NULL) {
CFNumberRef widthNum  = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
if (widthNum != NULL) {
    CFNumberGetValue(widthNum, kCFNumberCGFloatType, &width);
}

CFNumberRef heightNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNum != NULL) {
    CFNumberGetValue(heightNum, kCFNumberCGFloatType, &height);
}

CFRelease(imageProperties);
}

NSLog(@"Image dimensions: %.0f x %.0f px", width, height);

Andere Tipps

You can't at the NSFileManager level. You need to read and parse the file's contents - at least the header.

How you do that depends on the file's type.

The easiest thing to do would be to load it as an image and then interrogate the image.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top