如何在不下载的情况下从 url 获取图像的宽度和高度?

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

本文介绍了如何在不下载的情况下从 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,而我的图像在给定的框架中显示效果不佳.在这种情况下,当高度较大时,我想通过适当的高度比调整图像大小以匹配宽度 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?

推荐答案

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

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 获取图像的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在不下载的情况下从 url 获取图像的宽度和高度?

基础教程推荐