简体   繁体   English

UIImageView:如何显示缩放的图像?

[英]UIImageView: How to show scaled image?

I'm new to iOS app development and I begun to learn from this great tutorial: 我是iOS应用程序开发的新手,我开始从这个很棒的教程中学习:

Start Developing iOS Apps (Swift) 开始开发iOS应用(快速)

https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html#//apple_ref/doc/uid/TP40015214-CH6-SW1 https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html#//apple_ref/doc/uid/TP40015214-CH6-SW1

Everything looks great but when trying to load an image from "Camera roll" on the simulator, I get the image shown oversized and filling all the iPhone simulator screen instead of just showing into the UIImageView box as shown on tutorial images. 一切看起来都很不错,但是当尝试从模拟器上的“相机胶卷”加载图像时,我得到的图像显示为过大并充满了iPhone模拟器的所有屏幕,而不仅仅是显示在UIImageView框中,如教程图像所示。

The funny fact is that also downloading the correct project provided at the end of the lesson (see the bottom of the page) I get the same issue. 有趣的事实是,也下载了课程结束时提供的正确项目(请参阅页面底部),我也遇到了同样的问题。

Googling around I get some ideas to insert a: 谷歌搜索我有一些想法插入:

    // The info dictionary may contain multiple representations of the image. You want to use the original
    guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }

    // Set photoImageView to display the selected image
    photoImageView.image = selectedImage
    photoImageView.contentMode = UIViewContentMode.scaleAspectFit

    // Dismiss the picker
    dismiss(animated: true, completion: nil)

after loading image from the camera roll, but lead to no results... 从相机胶卷加载图像后,但没有结果...

I'm using Xcode 9.2 with deployment target 11.2 and iPhone 8 plus as a target for the simulator. 我将Xcode 9.2与部署目标11.2和iPhone 8 plus用作模拟器的目标。

Any help is apreciated. 任何帮助都非常感谢。

It happens because the sample code is only restricting the proportion of the UIImageView (the 1:1 constraint you added in storyboard). 发生这种情况是因为示例代码仅限制UIImageView的比例(您在情节提要中添加的1:1约束)。 In this way, it will always keep your imageView squared but will not limit the size. 这样,它将始终使您的imageView保持平方,但不会限制大小。

As the sample tells, you entered a placeholder size, that is only kept until you set some image on it. 如样本所示,您输入了一个占位符大小,该大小将一直保留到您在其上放置一些图像为止。 When you enter a new image on the UIImageView it changes to the size of the image you set (in your case, probably a bigger image for the camera roll). 当您在UIImageView上输入新图像时,它将更改为您设置的图像的大小(在您的情况下,可能是相机胶卷的更大图像)。 That's probably why its getting so large. 这可能就是为什么它变得如此之大的原因。

I think the easy way to fix this to add other constraints in storyboard to limit the size (like actually size or border constraints). 我认为解决此问题的简单方法是在情节提要中添加其他约束以限制大小(例如实际大小或边框约束)。

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

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