简体   繁体   English

将相机中的图像缩放到UIImageView(并保持比例)

[英]Scale an image from the camera to a UIImageView (and keep ratio)

I want to display a picture taken by the camera and scale it down AND keep scale ratio. 我想显示相机拍摄的照片并将其缩小并保持缩放比例。

The different models of iPhones all have different resolution of photos. 不同型号的iPhone均具有不同的照片分辨率。

I've tried some assignments with bounds etc but no luck - giving me a lvaule requires error. 我已经尝试了一些带有边界等的作业,但是没有运气-给我一个小小的机会需要错误。

My image from the camera is called "imageA" and the UIImageView is "imageView" 我从相机拍摄的图像称为“ imageA”,UIImageView为“ imageView”

Any ideas would be appreciated. 任何想法,将不胜感激。

我认为您只需要设置用于显示图像“宽高比”的UIImageView的内容模式即可。

Those lvalue errors are happening because the bounds themselves are a property but the coordinates in them are struct members (even though the syntax is the same) - you can't directly get or set them. 发生这些左值错误是因为边界本身是一个属性,但是边界中的坐标是结构成员(即使语法是相同的),您也无法直接获取或设置它们。 So to change the bounds you'd want something like: 因此,要更改界限,您需要类似以下内容:

CGRect imageBounds = imageView.bounds;
imageBounds.x += 10;
imageView.bounds = imageBounds;

Calling sizeToFit on the UIImageView after setting the image should get it to automatically resize its frame around it. 设置图像后在UIImageView上调用sizeToFit可以使它自动调整其周围的框架大小。

And yes, per Walter's answer if you set the view's contentMode to UIViewContentModeScaleAspectFit that should get it to preserve the aspect ratio. 是的,按照Walter的回答,如果您将视图的contentMode设置为UIViewContentModeScaleAspectFit ,则应该使它保持纵横比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM