如何使用swift中的图像视图生成随机图像 [英] How to generate random image using image view in swift

查看:181
本文介绍了如何使用swift中的图像视图生成随机图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是按照树屋课程创建我的第一个Fun Fact app.In它们会生成一个随机数组引号。

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

需要:

我已经使用storyboard放置了图像视图。已经按下一个按钮随机数组引号将生成。但我需要的时候相同的按钮按下应该生成随机图像。我是swift的新手。!

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

这是 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]
    }

}

这是 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()
    }

}


推荐答案

解决方案主要是使用与随机文本相同的方法。总而言之,你应该有一个图像数组,以及一个选择随机图像的功能。然后从视图控制器中调用该函数。这种方法的一种可能实现是:

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:

将此数组添加到 FactBook

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

将此方法添加到 FactBook

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

并在你的viewcontroller中更改 showFunFact to:

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你应该在你的资源中拥有image1.png,image2.png ...

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

这篇关于如何使用swift中的图像视图生成随机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆