适当缩放UIImage以显示全屏而不失真



当我们在iphone上拍照时,图像是全屏显示的,顶部没有任何"灰条",即图像以320*500的帧显示。我想在我的应用中显示该图像,但应用的最大帧大小为320*480。因此,当我试图在我的应用程序中显示全屏图像时,它显示为拉伸图像。我尝试了所有的contentMode选项,但它不起作用。那么,如何缩放图像或如何固定帧的大小,使图像显示为原来的样子,但在一个较小的帧没有任何失真或类似的东西,在"照片"的iphone应用程序?

当你拍照时,你实际上看不到全尺寸的照片,你只看到适合你的显示器的部分(照片的分辨率比iPhone的显示分辨率大)。因此,如果你想获得一个结果图像并全屏显示,那么你需要做一些简单的计算来缩放图像,使其比例正确:

// We know the desired resolution. It's full screen (320, 480) or (640, 960).
// Now we want to determine the destination imageView frame with maximum dimensions
// for it to fit the screen AND leave the image's proportions
float minScale = MIN(screenResolution.width/actualImgWidth, screenResolution.height/actualImgHeight);
// With minScale one side will fit full screen, and the other will occupy a bit smaller space than the screen allows
destImgView.bounds = CGRectMake(0, 0, minScale*actualImgWidth, minScale*actualImgHeight);
destImgView.image = actualImg;

最新更新