0

我是 iOS 应用程序开发的新手,我开始从这个很棒的教程中学习:

开始开发 iOS 应用程序 (Swift)

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

一切看起来都很棒,但是当尝试从模拟器上的“相机胶卷”加载图像时,我得到的图像显示过大并填满了所有 iPhone 模拟器屏幕,而不是只显示在 UIImageView 框中,如教程图像所示。

有趣的事实是,下载课程末尾提供的正确项目(见页面底部)我也遇到了同样的问题。

谷歌搜索我得到一些想法来插入:

    // 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)

从相机胶卷加载图像后,但没有结果...

我使用 Xcode 9.2 和部署目标 11.2 和 iPhone 8 plus 作为模拟器的目标。

任何帮助都将不胜感激。

4

1 回答 1

2

这是因为示例代码仅限制了 UIImageView 的比例(您在情节提要中添加的 1:1 约束)。这样,它将始终保持您的 imageView 平方,但不会限制大小。

如示例所示,您输入了一个占位符大小,该大小仅保留到您在其上设置了一些图像。当您在 UIImageView 上输入新图像时,它会更改为您设置的图像的大小(在您的情况下,可能是相机胶卷的更大图像)。这可能就是它变得如此之大的原因。

我认为解决此问题的简单方法是在情节提要中添加其他约束以限制大小(例如实际大小或边框约束)。

于 2018-02-08T17:51:58.767 回答