随机图像核心 - 按下按钮时显示背景

时间:2015-03-09 22:16:35

标签: ios swift

当我点按一个按钮时,我试图生成随机图像,但是当我点击按钮时,两个图像都会消失,只显示背景。

这是我的代码:

@IBAction func PickerTapped(sender: UIButton) {

    //randomize a number for the first image

    var firstrandomNumber = arc4random_uniform(7) + 1

    //construct a string with a random number

    var firstLeftHandImageString: String = String(format: "rud%i", firstrandomNumber)

    // set the left hand image view to the asset corresponding to the randomized number

    self.LeftHandImageView.image = UIImage(named: "firstLeftHandImageString")

    //randomize a number for the second image

    var secondrandomNumber = arc4random_uniform(7) + 1

    //construct a string with a random number 

    var secondRightHandImageString: String = String(format: "rud%i", secondrandomNumber)

    //set the right hand image view to the asset corresponding to the randomized number

    self.RightHandImageView.image = UIImage(named: "secondRightHandImageString")
}

1 个答案:

答案 0 :(得分:2)

self.RightHandImageView.image = UIImage(named: "secondRightHandImageString")

从名为" secondRightHandImageString"

的资产创建一个图像

如果你想要用你生成的字符串创建一个图像,你需要(注意变量名称周围没有双引号)

self.RightHandImageView.image = UIImage(named: secondRightHandImageString)