如何使用swift中的图像视图生成随机图像

时间:2015-10-04 11:08:32

标签: ios swift imageview

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

需要:

我已经使用storyboard放置了图像视图。已经按下一个pressed随机数组引号将生成。但是我需要在同一个按钮factbook.swift生成一个随机图像。我是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() } }

{{1}}

2 个答案:

答案 0 :(得分:4)

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

将此数组添加到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更改为:

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

        imgV.image = faceBook.randomFactImage()
    }

你应该在你的资源中拥有image1.png,image2.png ......

答案 1 :(得分:0)

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