Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get width of a resized image after UIViewContentModeScaleAspectFit

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

like image 263
Sergey Grischyov Avatar asked Feb 01 '13 13:02

Sergey Grischyov


2 Answers

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; 
like image 150
Mikhail Avatar answered Oct 02 '22 23:10

Mikhail


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) 
like image 44
iagomr Avatar answered Oct 02 '22 22:10

iagomr