How to generate random image using image view in s

2020-05-01 23:55发布

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 using storyboard.Already when pressing one button random array quotes will generate.But i need when that same button pressed a random image should generate.I am new to swift .!

This is factbook.swift

struct FactBook {
    // stored in arry to show all quotes
    let factsArray = [
    "You have to dream before your dreams can come true.",

        "To succeed in your mission, you must have single-minded devotion to your goal.",


         "You have to dream before your dreams can come true.",

         "Love your job but don’t love your company, because you may not know when your company stops loving you.",


         "Failure will never overtake me if my definition to succeed is strong enough.",




    ]


    //make a random quote

    func randomFact() -> String {

//        
//        let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
//       

        let unsignedArrayCount = UInt32(factsArray.count)
        let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
        let randomNumber = Int(unsignedRandomNumber)

//        
//        let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
//        let randomNumber = Int(signedRandomNumber)


        return factsArray[randomNumber]
    }

}

This is viewcontroller.swift

class ViewController: UIViewController {

    @IBOutlet weak var funFactLabel: UILabel!
    @IBOutlet weak var funFactButton: UIButton!

    @IBOutlet weak var imgV: UIImageView!

    let factBook = FactBook()
    let colorWheel = ColorWheel()

    //method to define

    //        let yourImage = UIImage(named: "apj")
    //        let imageview = UIImageView(image: yourImage)


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        funFactLabel.text = factBook.randomFact()

         self.view.backgroundColor = UIColor(patternImage: UIImage(named: "apj")!)
        //        let yourImage = UIImage(named: "apj")
//        let imageview = UIImageView(image: yourImage)
//        self.view.addSubview(imageview)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func showFunFact() {
        let randomColor = colorWheel.randomColor()
        view.backgroundColor = randomColor
        funFactButton.tintColor = randomColor
        //funFactButton.tintColor = clearcolor

        funFactLabel.text = factBook.randomFact()
    }

}

2条回答
放我归山
2楼-- · 2020-05-02 00:18

The solution mainly is to use the same approach you have done with the random text. So to sum up, you should have an array of the images, and a function to select a random image. Then call that function from your view controller. A possible implementation to this approach is:

Add this array to your FactBook

let factsImagesArray = [
    "image1.png",
        "image2.png",
         "image3.png",
         "image4.png",
         "image5.png",
    ]

Add this method to your FactBook

func randomFactImage() -> UIImage {
    let unsignedArrayCount = UInt32(factsImageArray.count)
    let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
    let randomNumber = Int(unsignedRandomNumber)
    return UIImage(named: factsImageArray[randomNumber])!
}

and in your viewcontroller change showFunFact to:

@IBAction func showFunFact() {
        let randomColor = colorWheel.randomColor()
        view.backgroundColor = randomColor
        funFactButton.tintColor = randomColor
        funFactLabel.text = factBook.randomFact()

        imgV.image = faceBook.randomFactImage()
    }

Ofc you should have the image1.png, image2.png ... in your resources

查看更多
Evening l夕情丶
3楼-- · 2020-05-02 00:26
@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
}
查看更多
登录 后发表回答