在UIViewContentModeScaleAspectFit之后获取调整大小的图像的宽度(Get width of a resized image afte

753 阅读1分钟

我有个UIImageView,我把一个图像,我调整大小这样:

UIImageView * attachmentImageNew = [[ UIImageView alloc] initWithFrame:CGRectMake(5.56.5245134)]; 
 attachmentImageNew.image = actualImage; 
 attachmentImageNew.backgroundColor = [UIColor redColor]; 
 attachmentImageNew.contentMode = UIViewContentModeScaleAspectFit; 

我试图在我的 UIImageView中获得调整大小的图片的宽度通过这样做:

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

但它实际上返回了原始图片的宽度。

解决方案

 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;