简体   繁体   English

如何从ios中的URL获取图像大小

[英]How to get Image size from URL in ios

How can I get the size(height/width) of an image from URL in objective-C?如何从Objective-C中的URL获取图像的大小(高度/宽度)? I want my container size according to the image.我想要我的容器大小根据图像。 I am using AFNetworking 3.0.我正在使用AFNetworking 3.0. I could use SDWebImage if it fulfills my requirement.如果SDWebImage满足我的要求,我可以使用它。

Knowing the size of an image before actually loading it can be necessary in a number of cases.在许多情况下,在实际加载图像之前了解图像的大小可能是必要的。 For example, setting the height of a tableView cell in the heightForRowAtIndexPath method while loading the actual image later in the cellForRowAtIndexPath (this is a very frequent catch 22).例如,稍后在 cellForRowAtIndexPath 中加载实际图像时,在 heightForRowAtIndexPath 方法中设置 tableView 单元格的高度(这是一个非常常见的问题 22)。

One simple way to do it, is to read the image header from the server URL using the Image I/O interface:一种简单的方法是使用图像 I/O 接口从服务器 URL 读取图像标题:

#import <ImageIO/ImageIO.h>

NSMutableString *imageURL = [NSMutableString stringWithFormat:@"http://www.myimageurl.com/image.png"];

CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)[NSURL URLWithString:imageURL], NULL);
NSDictionary* imageHeader = (__bridge NSDictionary*) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
NSLog(@"Image header %@",imageHeader);
NSLog(@"PixelHeight %@",[imageHeader objectForKey:@"PixelHeight"]);

Swift 4.x斯威夫特 4.x
Xcode 12.x Xcode 12.x

func sizeOfImageAt(url: URL) -> CGSize? {
    // with CGImageSource we avoid loading the whole image into memory
    guard let source = CGImageSourceCreateWithURL(url as CFURL, nil) else {
        return nil
    }
    
    let propertiesOptions = [kCGImageSourceShouldCache: false] as CFDictionary
    guard let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, propertiesOptions) as? [CFString: Any] else {
        return nil
    }
    
    if let width = properties[kCGImagePropertyPixelWidth] as? CGFloat,
       let height = properties[kCGImagePropertyPixelHeight] as? CGFloat {
        return CGSize(width: width, height: height)
    } else {
        return nil
    }
}

you can try like this:你可以这样尝试:

NSData *data = [[NSData alloc]initWithContentsOfURL:URL]; 
UIImage *image = [[UIImage alloc]initWithData:data];
CGFloat height = image.size.height;
CGFloat width = image.size.width;

Use Asynchronous mechanism called GCD in iOS to dowload image without affecting your main thread.使用 iOS 中称为 GCD 的异步机制来下载图像,而不会影响您的主线程。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Download IMAGE using URL
    NSData *data = [[NSData alloc]initWithContentsOfURL:URL];

    // COMPOSE IMAGE FROM NSData
    UIImage *image = [[UIImage alloc]initWithData:data];

    dispatch_async(dispatch_get_main_queue(), ^{
        // UI UPDATION ON MAIN THREAD
        // Calcualte height & width of image
        CGFloat height = image.size.height;
        CGFloat width = image.size.width;

    });
});

For Swift 4 use this:对于Swift 4使用这个:

let imageURL = URL(string: post.imageBigPath)!
let source = CGImageSourceCreateWithURL(imageURL as CFURL,     
let imageHeader = CGImageSourceCopyPropertiesAtIndex(source!, 0, nil)! as NSDictionary;
print("Image header: \(imageHeader)")

The header would looks like:标题看起来像:

Image header: { ColorModel = RGB;图像标题:{ ColorModel = RGB; Depth = 8;深度 = 8; PixelHeight = 640;像素高度 = 640; PixelWidth = 640;像素宽度 = 640; "{Exif}" = { PixelXDimension = 360; “{Exif}” = { PixelXDimension = 360; PixelYDimension = 360;像素Y维度 = 360; }; }; "{JFIF}" = { DensityUnit = 0; “{JFIF}” = { 密度单位 = 0; JFIFVersion = ( 1, 0, 1 ); JFIFVersion = ( 1, 0, 1 ); XDensity = 72; X密度 = 72; YDensity = 72; Y密度 = 72; }; }; "{TIFF}" = { Orientation = 0; “{TIFF}” = { 方向 = 0; }; }; } }

So u can get from it the Width, Height.所以你可以从中得到宽度,高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM