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

发布于 2024-10-06 16:49:03 字数 181 浏览 0 评论 0原文

我想显示相机拍摄的照片并将其缩小并保持比例。

不同型号的 iPhone 所拍摄的照片分辨率不同。

我尝试过一些带有边界等的作业,但没有运气 - 给我一个 lvaule 需要错误。

我的相机图像称为“imageA”,UIImageView 是“imageView”

任何想法将不胜感激。

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.

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"

Any ideas would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅冕 2024-10-13 16:49:03

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

I think you just need to set the content mode of the UIImageView you are using to display the image to "aspect fit".

青巷忧颜 2024-10-13 16:49:03

发生这些左值错误是因为边界本身是一个属性,但其中的坐标是结构成员(即使语法相同) - 您无法直接获取或设置它们。因此,要更改边界,您需要类似以下内容:

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

在设置图像后在 UIImageView 上调用 sizeToFit 应该让它自动调整其周围框架的大小。

是的,根据沃尔特的回答,如果您将视图的 contentMode 设置为 UIViewContentModeScaleAspectFit ,应该让它保留宽高比。

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.

And yes, per Walter's answer if you set the view's contentMode to UIViewContentModeScaleAspectFit that should get it to preserve the aspect ratio.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文