Come di iOS 4.0, iOS SDK include il CGImageSource...
functions (nel quadro ImageIO). È un'API molto flessibile per interrogare i metadati senza caricare l'immagine nella memoria. Ottenere le dimensioni in pixel di un'immagine dovrebbe funzionare come questo (assicurarsi di includere l'ImageIO.framework nel vostro target):
#import <ImageIO/ImageIO.h>
NSURL *imageFileURL = [NSURL fileURLWithPath:...];
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);
CFRelease(imageSource);
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);
}
// Check orientation and flip size if required
CFNumberRef orientationNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyOrientation);
if (orientationNum != NULL) {
int orientation;
CFNumberGetValue(orientationNum, kCFNumberIntType, &orientation);
if (orientation > 4) {
CGFloat temp = width;
width = height;
height = temp;
}
}
CFRelease(imageProperties);
}
NSLog(@"Image dimensions: %.0f x %.0f px", width, height);
(tratto da "Programmazione con Quartz" di Gelphman e Laden, elencando 9.5, pagina 228)
fonte
2010-11-13 00:06:38
Questa è una buona domanda. Android fornisce un mezzo per farlo, ma non conosco una soluzione iOS. EDIT: questo è stato chiesto prima. http://stackoverflow.com/questions/1551300/get-size-of-image-without-loading-in-memory – Justin
ho cercato prima, ma non riuscivo a trovarlo! grazie molto! – koda