iOS获取网络图片的宽、高

先导入 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);

}



你可能感兴趣的:(ios,iOS获取图片的宽高)