簡體   English   中英

CGImageSourceRef imageSource = CGImageSourceCreateWithURL返回NULL

[英]CGImageSourceRef imageSource = CGImageSourceCreateWithURL returns NULL

我想獲取圖像的大小而不加載它,所以我正在使用以下代碼

-(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圖片的有效鏈接

您是否啟用了ARC? 我沒有,我的工作代碼如下:

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

注意缺少__bridge 完成后,我在imageSource上調用CFRelease

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM