Random images to (many) Image Views - ios

Sadly, I've got 36 UIImages and need to set a random image to each one.
My 6 images are named;
"Owl1"
"Owl2"
"Owl3"
"Owl4"
"Owl5"
"Owl6"
So, I want to set one random image to my 36 different UIImages. What is the best way to do this? An array? Here's my "try" so far.
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]
But I realized this will not work, and I can't make this code for all 36 images... Anyone got a better idea? ;-)

Tip: you can use a range + map to create an array of your images.
let images = (1...6).map { UIImage(named: "Owl\($0)") }
(1...6) produces a collection of Ints, from 1 to 6 (including 6), and with map we create a new instance of UIImage for each Int, using them for the naming - since you named your images in order, it's convenient. It's like doing a loop and appending a new intance of UIImage to an array inside the loop, using an index for the naming: "Owl1", "Owl2", etc.
If you also have your UIImageViews in an array, you can assign the images with a loop.
Here's an example (I didn't verify on Xcode but it should be close to what you need):
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]
}

You can have an array of image names, and an array of images to hold them..
var imageNames:[String] = ["Owl1", "Owl2"....etc]
var owlImages:[UIImage] = []
Then randomly append the images
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
}

Related

Split array into groups of different sizes

I need to split an array into groups of different sizes.
let objects = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
let sizes = [3,2,4]
The output should be [[1,2,3], [4,5], [6,7,8,9], [10,11,12], [13,14], [15]]
What's the easiest way to achieve this?
This is not the exact solution for your problem but to split the array into different sizes you can use something like below:
var index = 0
var count = 0
for size in sizes{
let newArray = objects[index..<size+index]
index = count > 0 ? size + sizes[count-1] : size //index = 3
count = count + 1
print(newArray)
}
This will split array into group of different sizes. You can modify the code to complete all the array elements. This basically loops once over the sizes Array.

swift: Same image used in different UIImageView?

I have done the following to assign an UIImage to different instance of UIView object. Inside my UIView, I have an UIImageView which holds the Image.
var transitionImageNames = ["1.png", "2.png", "3.png", "4.png"]
var count=0
for item in myList {
var transitionImageName = transitionImageNames[count]
item.transitionImageView.image = UIImage(named: transitionImageName)
// transitionImageName refers to 4 different images, let say 1,2,3,4.png
if (count<=3) {
count += 1
}
}
there are a number of different items in myList. I have printed out the item instance, all of them have different instance value. However, I found that for the transitionImageView to be displayed, even if I set 1, 2, 3, 4.png to 4 different items in the myList, they all display the same image (which is the last one set)
Any idea on why?
I think you need something like this:
var transitionImageNames = ["1.png", "2.png", "3.png", "4.png"]
for (index, item) in myList.enumerated() {
item.transitionImageView.image = UIImage(named: transitionImageNames[index])
}

How to return number of views(ZLSwipeableViewSwif) like UITableview data source?

I am trying to implement , i cannot set number of views, i want to set only five view, which property i need to use before view is loading.
Library link enter link description here
self.swipeableView.numberOfActiveView = 5;
self.swipeableView.discardViews()
self.swipeableView.loadViews()
I am trying with their demo application.
You can Replace your colors array with other array like,
Declare one more array below that color array,
var imgArray = ["1","2","3","4"]
Now find the color array from the view did load and replace it with imgArray like,
swipeableView.nextView = {
if self.colorIndex < self.imgArray.count //self.colors.count
{
var cardFrame = CGRectMake(self.swipeableView.frame.origin.x, 0, self.swipeableView.frame.size.width, 250)
var cardView = CardView(frame: cardFrame)
var img : UIImageView = UIImageView(frame: cardView.bounds)
img.image = UIImage(named: self.imgArray[self.colorIndex])
cardView.addSubview(img)
self.colorIndex++
....
....
}
This working fine at my end so hope this will help you.

need to random an array of images and words. Arc4random_uniform

I'm working on a project of shapes and words. I currently have two arrays. One array is of images, the other array is just a set of strings that describes the first array of images. I'm using 4 buttons to display 4 items in the "string" array. Can someone please tell me how can I utilize arc4random but also make sure one of my 4 buttons display the name of the image.
Below is a quick example of my arrays
var myShapes = ["circle", "square", "rectangle"]
var images = ["circle.png", "square.png", "rectangle.png"]
How do I utilize arc4Random but make sure one item from each array always equals each other?
Adding information for the function to randomize my images.
func randomImage() -> UIImage {
let arrayCount = UInt32(myImages.count)
let unsignedRandomNumber = arc4random_uniform(arrayCount)
let randomNumber = Int(unsignedRandomNumber)
return UIImage(named: myImages[randomNumber])!
}
Below is how I'm doing the random text
let randomDisplayText = buttonDisplayText[Int(arc4random_uniform(UInt32(buttonDisplayText.count)))]

Animation by PNG frames

I have several PNG images that if it will be presented one after the other they will create a short animation.
My question is -
Is possible to create an animation with several PNG images, by displaying them one after the other?
Yes you can create animation with pngs,
let animationImagesArray : [UIImage] = [<Add images>]
imageView.animationImages = animationImagesArray
imageView.startAnimating()
You can also set repeat count and animation duration as well.
Update
To load sequence of images via loop its better you name them in a sequence some thing like this (animationImage1.png, animationImage2.png...)
for i in 0..<20
{
let name = "\(prefix)_\(i).png"
let image = UIImage(named: name)!
images.append(image)
}

Resources