Home > Software engineering >  Swift: How to store UIImage and its description in a collection?
Swift: How to store UIImage and its description in a collection?

Time:11-30

I need to store these in a collection. Then I will have two buttons "Previous" and "Next". If we reach the end of set, it should start back at beginning or jump to the end.

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.
}

}

Please help debug the code.

Thanks!

CodePudding user response:

You need to update these methods

@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()
}

This will let you to have infinite loop.

EDIT// Crash fix

Inside photoCollection you used UImage(named: "nameOfImage")!, this initializer can return nil if it will not found image with that name, and as you used force unwrapping you app crashes with that error. First of all dont use force unwrapping, it is bad practice, It is used in some rare cases.

How to do that more safe way?

Change your collection to this -- >

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"] ] 

Then change your showImage() method to this

 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
    }

Know your app will not crash even if image with some name will not be found. But you need to check all your images and their names for correct work of app.

  • Related