How to generate random image using image view in swift

前端 未结 2 1819
日久生厌
日久生厌 2021-01-28 08:37

I just followed treehouse course and create my first Fun Fact app.In that they generate a random array quotes.

Needed:

I have placed image view

2条回答
  •  故里飘歌
    2021-01-28 08:51

    @IBAction func randomimage(sender: AnyObject)
    {
        //list of Images in array 
        let image : NSArray = [ UIImage(named: "1.jpg")!,
            UIImage(named: "2.jpg")!,
            UIImage(named: "3.jpg")!,
            UIImage(named: "4.jpg")!,
            UIImage(named: "5.jpg")!,
            UIImage(named: "6.jpg")!,
            UIImage(named: "7.jpg")!]
    
        //random image generating method
        let imagerange: UInt32 = UInt32(image.count)
        let randomimage = Int(arc4random_uniform(imagerange))
        let generatedimage: AnyObject = image.objectAtIndex(randomimage)
        self.myimage.image = generatedimage as? UIImage
    }
    

提交回复
热议问题