Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 随机图像到(多个)图像视图_Ios_Swift_Image_Uiimage - Fatal编程技术网

Ios 随机图像到(多个)图像视图

Ios 随机图像到(多个)图像视图,ios,swift,image,uiimage,Ios,Swift,Image,Uiimage,不幸的是,我有36个UIImage,需要为每个UIImage设置一个随机图像 我的6个图像被命名 "Owl1" "Owl2" "Owl3" "Owl4" "Owl5" "Owl6" 所以,我想将一个随机图像设置为36个不同的UIImage。最好的方法是什么?阵列?这是我目前为止的尝试 var images: [UIImage] = [ UIImage(named: "Owl1")!, UIImage(named: "Owl2")!, UIImage(named: "Owl3")!, UIIma

不幸的是,我有36个UIImage,需要为每个UIImage设置一个随机图像

我的6个图像被命名

"Owl1"
"Owl2"
"Owl3"
"Owl4"
"Owl5"
"Owl6"
所以,我想将一个随机图像设置为36个不同的UIImage。最好的方法是什么?阵列?这是我目前为止的尝试

var images: [UIImage] = [
UIImage(named: "Owl1")!,
UIImage(named: "Owl2")!,
UIImage(named: "Owl3")!,
UIImage(named: "Owl4")!,
UIImage(named: "Owl5")!,
UIImage(named: "Owl6")!
]

var randomUIImage = [Image1, Image2, Image3, Image4, Image5...]
        randomUIImage.shuffleInPlace()

randomUIImage[0].image = images[0]
randomUIImage[1].image = images[1]

但我意识到这是行不通的,而且我不能为所有36张图片编写代码。。。谁有更好的主意-

您可以有一个图像名称数组,也可以有一个图像数组来保存它们

var imageNames:[String] = ["Owl1", "Owl2"....etc]
var owlImages:[UIImage] = []
然后随机添加图像

for index in 0...imageNames.count - 1 {
    var randomInt = Int(arc4random_uniform(UInt32(imageNames.count)) //a random int from 0 to the size of your array
    owlImages.append(UIImage(named: imageNames[randomInt] //add the random image to the array
}

您可以有一个图像名称数组,也可以有一个图像数组来保存它们

var imageNames:[String] = ["Owl1", "Owl2"....etc]
var owlImages:[UIImage] = []
然后随机添加图像

for index in 0...imageNames.count - 1 {
    var randomInt = Int(arc4random_uniform(UInt32(imageNames.count)) //a random int from 0 to the size of your array
    owlImages.append(UIImage(named: imageNames[randomInt] //add the random image to the array
}

提示:您可以使用范围+贴图创建图像数组

let images = (1...6).map { UIImage(named: "Owl\($0)") }
1…6生成一个整数集合,从1到6,包括6,使用map,我们为每个整数创建一个新的UIImage实例,使用它们进行命名-因为您按顺序命名图像,这很方便。这就像做一个循环,并在循环内的数组中添加一个新的UIImage元素,使用一个索引进行命名:Owl1、Owl2等等

如果您的UIImageView也在一个数组中,则可以使用循环指定图像

下面是一个我没有在Xcode上验证的示例,但它应该接近您所需要的:

for view in imageViewsArray { // the array with the 36 imageViews
    // a random index for the array of 6 images
    let randomIndex = Int(arc4random_uniform(UInt32(images.count))
    // assign the randomly chosen image to the image view
    view.image = images[randomIndex]
}

提示:您可以使用范围+贴图创建图像数组

let images = (1...6).map { UIImage(named: "Owl\($0)") }
1…6生成一个整数集合,从1到6,包括6,使用map,我们为每个整数创建一个新的UIImage实例,使用它们进行命名-因为您按顺序命名图像,这很方便。这就像做一个循环,并在循环内的数组中添加一个新的UIImage元素,使用一个索引进行命名:Owl1、Owl2等等

如果您的UIImageView也在一个数组中,则可以使用循环指定图像

下面是一个我没有在Xcode上验证的示例,但它应该接近您所需要的:

for view in imageViewsArray { // the array with the 36 imageViews
    // a random index for the array of 6 images
    let randomIndex = Int(arc4random_uniform(UInt32(images.count))
    // assign the randomly chosen image to the image view
    view.image = images[randomIndex]
}

埃里克,你总是把我捧起来;不熟悉该代码,如何使用它将图像实际应用于图像视图-顺便说一句,你知道我在情节提要中只有6个Owl图像和36个图像视图吗-@我已经更新了我的答案。当然,还有愚蠢的打字错误P你总是把我捧起来,埃里克;不熟悉该代码,如何使用它将图像实际应用于图像视图-顺便说一句,你知道我在情节提要中只有6个Owl图像和36个图像视图吗-@我已经更新了我的答案。当然,还有愚蠢的打字错误P