如何在UIImageView中获取缩放的UIImage的大小?

UIImageViewimage.size属性给出了原始UIImage的大小。 我想找出放在UIImageView中的自动缩放图像的大小(通常小于原始大小)。

例如,我将图像设置为Aspect Fit 。 现在我想知道它的新的高度和宽度在屏幕上,所以我可以准确地绘制新的缩放图像。

有没有办法做到这一点,而不是根据UIImageView的大小和UIImage的原始大小(基本上是逆向工程的缩放)自己搞清楚?

 -(CGRect)frameForImage:(UIImage*)image inImageViewAspectFit:(UIImageView*)imageView { float imageRatio = image.size.width / image.size.height; float viewRatio = imageView.frame.size.width / imageView.frame.size.height; if(imageRatio < viewRatio) { float scale = imageView.frame.size.height / image.size.height; float width = scale * image.size.width; float topLeftX = (imageView.frame.size.width - width) * 0.5; return CGRectMake(topLeftX, 0, width, imageView.frame.size.height); } else { float scale = imageView.frame.size.width / image.size.width; float height = scale * image.size.height; float topLeftY = (imageView.frame.size.height - height) * 0.5; return CGRectMake(0, topLeftY, imageView.frame.size.width, height); } } 

这个简单的function将计算方面适合后的图像大小:

 -(CGSize)imageSizeAfterAspectFit:(UIImageView*)imgview{ float newwidth; float newheight; UIImage *image=imgview.image; if (image.size.height>=image.size.width){ newheight=imgview.frame.size.height; newwidth=(image.size.width/image.size.height)*newheight; if(newwidth>imgview.frame.size.width){ float diff=imgview.frame.size.width-newwidth; newheight=newheight+diff/newheight*newheight; newwidth=imgview.frame.size.width; } } else{ newwidth=imgview.frame.size.width; newheight=(image.size.height/image.size.width)*newwidth; if(newheight>imgview.frame.size.height){ float diff=imgview.frame.size.height-newheight; newwidth=newwidth+diff/newwidth*newwidth; newheight=imgview.frame.size.height; } } NSLog(@"image after aspect fit: width=%f height=%f",newwidth,newheight); //adapt UIImageView size to image size //imgview.frame=CGRectMake(imgview.frame.origin.x+(imgview.frame.size.width-newwidth)/2,imgview.frame.origin.y+(imgview.frame.size.height-newheight)/2,newwidth,newheight); return CGSizeMake(newwidth, newheight); } 

这个简单的函数将计算图像的大小:

 [imageView setFrame:AVMakeRectWithAspectRatioInsideRect(image.size, imageView.frame)]; 

有关更多详细信息,请参阅AV基础函数参考 。

一个适合cncool答案 ,作为一个UIImageView扩展迅速,也许它可能是有用的人:

 extension UIImageView{ func frameForImageInImageViewAspectFit() -> CGRect { if let img = self.image { let imageRatio = img.size.width / img.size.height; let viewRatio = self.frame.size.width / self.frame.size.height; if(imageRatio < viewRatio) { let scale = self.frame.size.height / img.size.height; let width = scale * img.size.width; let topLeftX = (self.frame.size.width - width) * 0.5; return CGRectMake(topLeftX, 0, width, self.frame.size.height); } else { let scale = self.frame.size.width / img.size.width; let height = scale * img.size.height; let topLeftY = (self.frame.size.height - height) * 0.5; return CGRectMake(0, topLeftY, self.frame.size.width, height); } } return CGRectMake(0, 0, 0, 0) } } 

由于您已将图像视图设置为Aspect Fit,因此可以简单地将图像视图的高度和宽度以及原始图像的高度和宽度一起取出,并计算缩放图像的高度和宽度。

也就是说,这样做的更好的方法是有一个自定义视图,而不是UIImageView。 然后,在自定义视图中自己绘制图像。 这样你可以控制它的每一个方面。

如何从它的框架中获取UIImageView的大小? 即imageView.frame?

如果在保持图像宽高比的情况下填充了帧,则图像视图帧将不会与图像大小相同。