当前位置: 代码迷 >> 综合 >> iOS获取网络图片的宽、高
  详细解决方案

iOS获取网络图片的宽、高

热度:84   发布时间:2024-01-20 19:38:30.0

先导入 ImageIO框架

#import <ImageIO/ImageIO.h>

- (CGSize)getImageSizeWithURL:(NSURL *)url

{

    CGImageSourceRef image = CGImageSourceCreateWithURL((CFURLRef)url, NULL);

    CGFloat width = 0.0f, height = 0.0f;

    

    if (image)

    {

        CFDictionaryRef imageAcc = CGImageSourceCopyPropertiesAtIndex(image, 0, NULL);

        

        if (imageAcc != NULL)

        {

            CFNumberRef widthNumber  = CFDictionaryGetValue(imageAcc, kCGImagePropertyPixelWidth);

            if (widthNumber != NULL) {

                CFNumberGetValue(widthNumber, kCFNumberFloatType, &width);

            }

            

            CFNumberRef heightNumber = CFDictionaryGetValue(imageAcc, kCGImagePropertyPixelHeight);

            if (heightNumber != NULL) {

                CFNumberGetValue(heightNumber, kCFNumberFloatType, &height);

            }

            

            CFRelease(imageAcc);

        }

        CFRelease(image);

        NSLog(@"Image dimensions: %.0f x %.0f px", width, height);

    }

    return CGSizeMake(width, height);

}



  相关解决方案