【问题标题】:Swift: How to store UIImage and its description in a collection?Swift:如何将 UIImage 及其描述存储在集合中?
【发布时间】:2022-01-05 10:44:39
【问题描述】:

我需要将这些存储在一个集合中。然后我将有两个按钮“上一个”和“下一个”。如果我们到达集合的结尾,它应该从头开始或跳到结尾。

class ViewController: UIViewController {


var photoCollection: [[String:Any]] = [
        ["image": UIImage(named: "Sea house")!, "text": "sea house"]
        // Other photos
    ]



@IBOutlet weak var photo: UIImageView!
@IBOutlet weak var Text: UILabel!



func showImage() {
    
    photo.image = photoCollection[count]["image"] as! UIImage
    Text.text = photoCollection[count]["text"] as! String
    }




@IBAction func Previous(_ sender: UIButton)
{
    guard count > 0 else {return}
            count -= 1
            showImage()
    
}


@IBAction func Next(_ sender: UIButton) {
   
    guard count < photoCollection.count - 1 else {return}
            count += 1
            showImage()
        }
    



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

}

请帮助调试代码。

谢谢!

【问题讨论】:

  • 什么是你所说的“收藏”? UICollectionView?我没有看到任何一行表明您正在使用带有 UICollectionView 对象的 photoCollection 家伙。
  • 它是 UIImage“图像视图”,我有 10 张图像“每张都有文字描述”,需要将它们存储在一个集合中。

标签: swift function button collections uiimage


【解决方案1】:

你需要更新这些方法

@IBAction func Previous(_ sender: UIButton) {
    count = count > 0 ? count - 1 : photoCollection.count - 1
    showImage()
}
    
@IBAction func Next(_ sender: UIButton) {
    count = count < photoCollection.count - 1 ? count + 1 : 0
    showImage()
}

这会让你有无限循环。

EDIT// 崩溃修复

photoCollection 中,您使用了 UImage(named: "nameOfImage")!,如果找不到具有该名称的图像,则此初始化程序可以返回 nil,并且当您使用强制展开时,应用程序会因该错误而崩溃。 首先不要使用强制展开,这是不好的做法,在极少数情况下使用。

怎样做更安全?

把你的收藏改成这个 -->

var photoCollection: [[String:Any?]] = [
                                ["image": UIImage(named: "P1"), "text": "City Tavern Bathroom"],
                                ["image": UIImage(named: "P2"), "text": "Shafer Trail, Island in the Sky District"],
                                ["image": UIImage(named: "P3"), "text": "Rivers Bend Group Campground"],
                                ["image": UIImage(named: "P4"), "text": "Delta at Lake Mead"],
                                ["image": UIImage(named: "P5"), "text": "Deer between Sequoias"],
                                ["image": UIImage(named: "P6"), "text": "Arlington House, The Robert E. Lee Memorial"],
                                ["image": UIImage(named: "P7"), "text": "Brink of the Lower Falls of the Yellowstone River"],
                                ["image": UIImage(named: "P8"), "text": "Garage Exterior"],
                                ["image": UIImage(named: "P9"), "text": "DSCF1199"],
                                ["image": UIImage(named: "P10"), "text": "The Bi-national Formation"] ] 

然后将您的 showImage() 方法更改为此

 func showImage() {
        guard let image = photoCollection[count]["image"] as? UIImage,
              let description = photoCollection[count]["text"] as? String else {
            return
        }
        photo.image = image
        Text.text = description
    }

知道即使找不到带有某个名称的图像,您的应用也不会崩溃。 但是您需要检查所有图像及其名称以确保应用程序正常工作。

【讨论】:

  • 嗨,我仍然收到相同的错误消息。我的函数“show Image()”有什么问题吗?
  • @Spiky 我添加了与崩溃相关的部分。
  • 感谢您的帮助。我尝试了你建议我做的事情,这次我仍然有同样的错误信息,我的图像部分很好,但这次是在隐式展开可选值时发现的“Text.text = description”为 nil。
  • 别管男人。我已经解决了问题,非常感谢你的帮助,你是最棒的。
猜你喜欢
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多