簡體   English   中英

UIImageView:如何顯示縮放的圖像?

[英]UIImageView: How to show scaled image?

我是iOS應用程序開發的新手,我開始從這個很棒的教程中學習:

開始開發iOS應用(快速)

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用作模擬器的目標。

任何幫助都非常感謝。

發生這種情況是因為示例代碼僅限制UIImageView的比例(您在情節提要中添加的1:1約束)。 這樣,它將始終使您的imageView保持平方,但不會限制大小。

如樣本所示,您輸入了一個占位符大小,該大小將一直保留到您在其上放置一些圖像為止。 當您在UIImageView上輸入新圖像時,它將更改為您設置的圖像的大小(在您的情況下,可能是相機膠卷的更大圖像)。 這可能就是為什么它變得如此之大的原因。

我認為解決此問題的簡單方法是在情節提要中添加其他約束以限制大小(例如實際大小或邊框約束)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM