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?
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)")
}
}
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With