【问题标题】:CGImageSourceRef imageSource = CGImageSourceCreateWithURL returns NULLCGImageSourceRef imageSource = CGImageSourceCreateWithURL 返回 NULL
【发布时间】:2013-05-23 12:11:28
【问题描述】:

我想在不加载图像的情况下获取图像的大小,所以我使用下面的代码

-(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 的有效链接

【问题讨论】:

  • 如果你得到了这个问题的答案,请告诉我。我也面临同样的问题。

标签: objective-c cgimagesource


【解决方案1】:

您是否启用了 ARC?我没有,我的工作代码如下:

NSURL * imageFileURL = [NSURL fileURLWithPath:fp];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);

注意__bridge 的缺失。顺便说一句,我在imageSource 上打电话给CFRelease

【讨论】:

    猜你喜欢
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多