문제

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];
도움이 되었습니까?

해결책

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);

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top