如何在不下载的情况下从url获取图像的宽度和高度? [英] how to get width and height of image from url without downloading?

查看:171
本文介绍了如何在不下载的情况下从url获取图像的宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SDWebImage来显示单元内的图像.但这是我在下面的代码中完美实现的UImageView框架:

I'm using SDWebImage for showing images inside cells. But it is perfect mached to frame of UImageView that I'm doing in code below:

NSString * s =[NSString stringWithFormat:@"url of image to show"];
        NSURL *url = [NSURL URLWithString:s];
        [cell.shopImageView sd_setImageWithURL:url];

我的UIImageView尺寸为50x50.

My UIImageView is size 50x50.

例如,来自url的图像的尺寸为990x2100,而我的图像在给定的框架中无法很好地显示. 在这种情况下,当high更大时,我想通过适当的高度比例来调整图像大小以匹配宽度50.

For example image from url is size 990x2100 and my image is not displaying well in the frame given. In this case when hight is bigger, I want to resize my image by proper height ratio to match width 50.

有没有一种方法可以从url中检查图像的大小,而无需下载并以糟糕的方式分配内存?

Is there a way to check image's size from url without downloading it and allocating memory in awful way?

推荐答案

您可以在 Swift 3.0 中使用以下代码从URL标头中获取此数据

You can fetch this data from URL header, in Swift 3.0 use below code

 if let imageSource = CGImageSourceCreateWithURL(url! as CFURL, nil) {
     if let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary? {
          let pixelWidth = imageProperties[kCGImagePropertyPixelWidth] as! Int
          let pixelHeight = imageProperties[kCGImagePropertyPixelHeight] as! Int
          print("the image width is: \(pixelWidth)")
          print("the image height is: \(pixelHeight)")
       }
   }

这篇关于如何在不下载的情况下从url获取图像的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆