UIImageView:如何显示缩放图像



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

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

https://developer.apple.com/library/content/referencelibrary/gettingstarted/devervevelingiosappsswift/workwithewithewithviewcontrollers.htmll#/apple_ref/paple_ref/doc/doc/doc/uid/uid/uid/uid/tp40015214-ch66-sw1

一切看起来都很棒,但是当试图从模拟器上加载图像时,我会得到超大显示的图像,并填充所有iPhone模拟器屏幕,而不仅仅是显示在UIImageView框中,如教程图像所示。/p>

有趣的事实是,还下载课程结束时提供的正确项目(请参阅页面的底部(我遇到了相同的问题。

谷歌搜索,我有一些想法插入:

    // 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作为模拟器的目标。

任何帮助都会被重组。

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

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

我认为,解决此问题以在情节板中添加其他约束以限制大小的简便方法(例如大小或边界约束(。

最新更新