问题
I want to get the Size of the image without loading it so i am using the below code
-(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;
}
The Problem is that imageSource is always NULL Does any body has any idea , why it is not working !!
Please Note that URL are Valid links to images JPG or PNG
回答1:
Do you have ARC enabled? I don't and my working code is as follows:
NSURL * imageFileURL = [NSURL fileURLWithPath:fp];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
Note the absence of __bridge
. I call CFRelease
on imageSource
when done btw.
来源:https://stackoverflow.com/questions/16713610/cgimagesourceref-imagesource-cgimagesourcecreatewithurl-returns-null