CGImageSourceRef imageSource = CGImageSourceCreateWithURL returns NULL

流过昼夜 提交于 2019-12-12 02:54:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!