简体   繁体   English

swift 3中如何使用UIImage数组为零?

[英]How to use UIImage in swift 3 the array comes nil?

I have one array of png images. 我有一组png图像。

iconeImage = [UIImage(named:"ion-home")!,UIImage(named:"ms-medkit")!,UIImage(named:"ion-edit")!,UIImage(named:"ion-clipboard")!,UIImage(named:"ion-clipboard")!,UIImage(named:"ion-waterdrop")!,UIImage(named:"ion-calendar")!]

and I have one function for table-view which set the image with this array elements 我有一个用于表视图的函数,该函数使用此数组元素设置图像

here is the code 这是代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell") as! MenuTableViewCell
        cell.imgIcon.image = iconeImage[indexPath.row]
        return cell
    }

But it gives me error that iconeImage has nil values. 但是它给我的错误是iconeImage具有nil值。 so can you suggest me solutions. 所以你能给我建议解决方案。 I am beginner in swift 3 我是Swift 3的初学者

Here is the screenshot 这是屏幕截图 忙碌的猫 ![two muppets][1] ![两个木偶] [1]

Instead of holding all images in memory I'd suggest you to store image names: 建议您不要存储图像名称,而不是将所有图像保存在内存中:

let iconImages = ["ion-home", "ms-medkit", "ion-edit", "ion-clipboard", "ion-waterdrop", "ion-calendar"]

And preload each image directly when you need it. 并在需要时直接预加载每个图像。 It will not affect on performance as it is small icons. 因为它是小图标,所以不会影响性能。 Also code will look cleaner. 代码也看起来更干净。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell") as! MenuTableViewCell
    cell.imgIcon.image = UIImage(named: iconImages[indexPath.row])
    return cell
}

Double check if all the assets are added to your project. 仔细检查是否所有资产都已添加到您的项目中。 If they are there, image should be displayed. 如果它们在那里,则应显示图像。

Try to avoid of using implicitly unwrapped optional (!) in your code. 尝试避免在代码中使用隐式展开的可选(!)。 Nil value will crash the app. 零值将使应用程序崩溃。 In your example, imageView.image is an optional, so you can set UIImage(named: "imageName") to it. 在您的示例中,imageView.image是可选的,因此您可以为其设置UIImage(named:“ imageName”)。

Also, you can try to clean build folder, clean project and reinstall the app. 另外,您可以尝试清理构建文件夹,清理项目并重新安装该应用程序。 Sometimes copying resources failed in Xcode. 有时在Xcode中复制资源失败。

Please try the below format 请尝试以下格式

  let img = iconeImage[indexPath.row]
    if let imgData = img {
        cell.imgView.image = UIImage(data:imgData)
    }

You can store your image names instead: 您可以改为存储图像名称:

let iconImageNames = ["ion-home","ms-medkit", ... , "ion-calendar")]

Later access them in your method: 以后使用您的方法访问它们:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell") as! MenuTableViewCell
    let name = iconeImage[indexPath.row] ?? yourDefaultImageName
    cell.imgIcon.image = UIImage(named: name)
    return cell
}

As an example 举个例子

I don't know exactly what is the issue. 我不知道到底是什么问题。

But when i have searching over stackoverflow. 但是当我搜索了stackoverflow时。 somewhere i have read that something problem with Assets.xcassets 我在某处读到Assets.xcassets有问题

then i got the error exact of this 然后我得到了错误的确切

And then solve the error with the help of that answer. 然后借助该答案解决错误。

and hopefully i resolved the error. 并希望我解决了错误。

meanwhile the problem is with assets folder. 同时问题出在资产文件夹中。

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

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