【问题标题】:How to get Image size from URL in ios如何从ios中的URL获取图像大小
【发布时间】:2016-09-21 08:42:35
【问题描述】:

如何从 Objective-C 中的 URL 获取图像的大小(高度/宽度)?我想要我的容器大小根据图像。我正在使用AFNetworking 3.0. 如果满足我的要求,我可以使用SDWebImage

【问题讨论】:

  • 在设置视图之前下载图像?
  • 不需要知道图像的大小。尝试创建 UIImageView 任何您想要的大小并更改其内容类型(scaletofill、aspectfill、aspectfit 等)
  • 您从哪个 URL 下载图像?很多图像服务API在获取的数据中提供了height/width属性(比如json)你可以看看API Document。
  • 什么要求!!请再次验证您的要求。我猜你错过了什么。
  • 感谢以上所有。实际上图像来自我自己的服务器。但是这些不是相同的分辨率。最小值为 800*800。但我需要知道图像大小才能相应地更新我的图像视图。

标签: objective-c afnetworking sdwebimage


【解决方案1】:

你可以这样试试:

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

【讨论】:

  • 这是一种同步方式。它会卡住 UI,直到它得到数据。
【解决方案2】:

在 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;

    });
});

【讨论】:

  • 我实际上并没有下载。我正在使用 AFNetworking 缓存,例如 [imageView setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:object] placeholderImage:[UIImage imageNamed:@"DefaultProductImage"]];
【解决方案3】:

在许多情况下,在实际加载图像之前了解图像的大小可能是必要的。例如,在 heightForRowAtIndexPath 方法中设置 tableView 单元格的高度,同时稍后在 cellForRowAtIndexPath 中加载实际图像(这是一个非常常见的问题 22)。

一种简单的方法是使用图像 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"]);

【讨论】:

  • 很好的答案!帮了我很多。谢谢!
【解决方案4】:

对于 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)")

标题看起来像:

图片标题:{ 颜色模型 = RGB; 深度 = 8; 像素高度 = 640; 像素宽度 = 640; "{Exif}" = { PixelXDimension = 360; PixelYDimension = 360; }; "{JFIF}" = { 密度单位 = 0; JFIFVersion = ( 1、 0, 1 ); X 密度 = 72; Y密度= 72; }; “{TIFF}”= { 方向 = 0; }; }

所以你可以从中得到宽度,高度。

【讨论】:

    【解决方案5】:

    Swift 4.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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 2013-07-17
      • 1970-01-01
      • 2013-09-18
      相关资源
      最近更新 更多