Get width of a resized image after UIViewContentMo

2019-03-08 10:29发布

I have my UIImageView and I put an image into it that I resize like this:

UIImageView *attachmentImageNew = [[UIImageView alloc] initWithFrame:CGRectMake(5.5, 6.5, 245, 134)];
attachmentImageNew.image = actualImage;
attachmentImageNew.backgroundColor = [UIColor redColor];
attachmentImageNew.contentMode = UIViewContentModeScaleAspectFit;

I tried getting the width of the resized picture in my UIImageView by doing this:

NSLog(@"Size of pic is %f", attachmentImageNew.image.size.width);

But it actually returns me the width of the original picture. Any ideas on how do I get the frame of the picture that I see on screen?

EDIT: Here's how my UIImageView looks, red area is its backgroundColor

enter image description here

6条回答
干净又极端
2楼-- · 2019-03-08 11:01

A solution in Swift:

let currentHeight = imageView.bounds.size.height
let currentWidth = imageView.bounds.size.width
let newWidth = UIScreen.mainScreen().bounds.width
let newHeight = (newWidth * currentHeight) / currentWidth

println(newHeight)
查看更多
Fickle 薄情
3楼-- · 2019-03-08 11:03

It has the reference of your original image, so always gives the same dimensions as of original image.

To get the dimensions of new image you have to check aspect ratio. I have derived a formula for my need using different images of different size using Preview that how it resizes image according to its aspect ratio.

查看更多
淡お忘
4楼-- · 2019-03-08 11:03

in your case:

float yourScaledImageWitdh =  attachmentImageNew.frame.size.height *  attachmentImageNew.image.size.width / attachmentImageNew.image.size.height
NSLog(@"width of resized pic is %f", yourScaledImageWitdh);

but you should also check, before, the original image proportion vs the imageView proportion, my line of code is good just in case the red area is added horizontally, not in case your original image proportion is wider than the imageView frame proportion

查看更多
对你真心纯属浪费
5楼-- · 2019-03-08 11:06

I don't know is there more clear solution, but this works:

float widthRatio = imageView.bounds.size.width / imageView.image.size.width;
float heightRatio = imageView.bounds.size.height / imageView.image.size.height;
float scale = MIN(widthRatio, heightRatio);
float imageWidth = scale * imageView.image.size.width;
float imageHeight = scale * imageView.image.size.height;
查看更多
smile是对你的礼貌
6楼-- · 2019-03-08 11:16

For Swift 2, you can use this code snippet to align an aspectFitted image to the bottom of the screen (sampled from earlier answers to this question -> thanks to you, guys)

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = CGFloat(screenSize.width)
let screenHeight = CGFloat(screenSize.height)

imageView.frame = CGRectMake(0, screenHeight - (imageView.image?.size.height)! , screenWidth, (imageView.image?.size.height)!)

let newSize:CGSize = getScaledSizeOfImage(imageView.image!, toSize: self.view.frame.size)

imageView.frame = CGRectMake(0, screenHeight - (newSize.height) , screenWidth, newSize.height)

func getScaledSizeOfImage(image: UIImage, toSize: CGSize) -> CGSize       
{
    let widthRatio = toSize.width/image.size.width
    let heightRatio = toSize.height/image.size.height
    let scale = min(widthRatio, heightRatio)
    let imageWidth = scale*image.size.width
    let imageHeight = scale*image.size.height
    return CGSizeMake(imageWidth, imageHeight)
}
查看更多
Juvenile、少年°
7楼-- · 2019-03-08 11:18

According to Apple's Documentation for UIViewContentModeScaleAspectFit

It Scales the content (In your case actualImage) to fit the size of the view (In your case attachmentImageNew) by maintaining the aspect ratio.

That means your image size ( after the scaling ) should be same as your UIImageView.

UPDATE :

One new Suggestion to you if you don't want to use UIViewContentModeScaleAspectFit and if you can fix the size of scaledImage then you can use below code to scale the image for fix newSize and then you can use the width to your code.

CGSize newSize = CGSizeMake(100.0,50.0);
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
查看更多
登录 后发表回答