我想在不加载图像的情况下获取图像的大小,所以我使用下面的代码
-(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 的有效链接