how to get width and height of image from url without downloading?

别等时光非礼了梦想. 提交于 2019-12-18 12:58:26

问题


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

My UIImageView is size 50x50.

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.

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


回答1:


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)")
       }
   }



回答2:


Try messing with the different contentMode options to get the look you want. One example could be cell.shopImageView.contentMode = UIViewContentModeScaleAspectFit; That will just make the image fit nicely, but won't actually resize the image view.

Here are your contentMode options: UIViewContentModes

An alternative could be something along the lines of this:

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

You can then set your imageView height/width depending on the ratio of the image's height/width.




回答3:


  • I don't know how to get the size of your image from URL without downloading the image.

  • But I can provide you some code snippet to make your UIImageView frame proportionally after the image is downloaded.

    NSData *data = [[NSData alloc]initWithContentsOfURL:URL]; // -- avoid this.
    
  • If you use the above method to download image it will block your UI. So please avoid it.

    [cell.shopImageView ....]; // -- avoid this method.
    
  • As you're using SDWebImage, I guess it will have some dedicated methods to download the image first. So you can use that method to download the image instead of using UIImageView categories method as you used above.

  • After you downloaded the image. Try something like below.

  • Code Snippet

  • Assume the image is downloaded and the object is 'theImage', and 'imageView' as your cell imageview

    float imageRatio = theImage.size.width/theImage.size.height;
    float widthWithMaxHeight = imageView.frame.size.height * imageRatio;
    float finalWidth, finalHeight;
    if (widthWithMaxHeight > imageView.frame.size.width) {
        finalWidth = imageView.frame.size.width;
        finalHeight = imageView.frame.size.width/imageRatio;
    } else {
        finalHeight = imageView.frame.size.height;
        finalWidth = imageView.frame.size.height * imageRatio;
    }
    
    [imageView setFrame:CGRectMake(xOffset, yOffset, finalWidth, finalHeight)];
    [imageView setImage:theImage];
    



回答4:


 let ImageArray =  ((arrFeedData[indexPath.row] as AnyObject).value(forKey: "social_media_images") as? NSArray)!
 var ImageURL: String =  ((ImageArray[0] as AnyObject) as? String)!
 ImageURL = ImageURL.addingPercentEscapes(using: String.Encoding.ascii)!
   let imageUrl = URL(string: ImageURL)
   let imageData = try Data(contentsOf: imageUrl!)
    let image = UIImage(data: imageData)
print("image height: \(image?.size.height)"
  print("image Width: \(image?.size.width)")


来源:https://stackoverflow.com/questions/24895333/how-to-get-width-and-height-of-image-from-url-without-downloading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!