画像をロードせずにサイズを取得したいので、以下のコードを使用しています
-(float)gettingimagedimensions:(NSString*)url{
NSURL *imageFileURL = [NSURL fileURLWithPath:url];
//CGImageSourceRef imageSource = CGImageSourceCreateWithURL(imageFileURL, NULL);
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)imageFileURL, NULL);
if (imageSource == NULL) {
// Error loading image
return 0;
}
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, kCFNumberFloatType, &width);
}
CFNumberRef heightNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNum != NULL) {
CFNumberGetValue(heightNum, kCFNumberFloatType, &height);
}
CFRelease(imageProperties);
}
NSLog(@"Image dimensions: %.0f x %.0f px", width, height);
return height;
}
問題は、imageSource が常に NULL であることです。
URL は画像 JPG または PNG への有効なリンクであることに注意してください