1

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

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

4

1 回答 1

1

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

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

注意没有__bridge. 顺便说一句,我打电话给CFReleaseimageSource

于 2013-07-16T16:05:48.127 回答