0

Currently creating an basic image slideshow application, To do this i have created an UIImage array and calling them with the following code.

let imageNames = (0...50).map
    {
        "\($0).JPG"
    }
    let image = UIImage(named: imageNames[0])

    imageView.animationImages = image

    imageView.animationDuration = 50
    imageView.startAnimating()

Was wondering if anyone would be able to offer some advice.

diddly101
  • 3
  • 3

2 Answers2

0

You need to add image name in array instead of UIImage .

let images: [String] = [
        "0.JPG",
         "1.JPG")!]

Get image from string of array

let image = UIImage(named: images[0])
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

The issue isn't how much memory the image takes on disk, it's about the memory required to address all the pixels on your screen. You don't really need to store the images before you use them. One image of less than 1MB will load very quickly.

Instead you just need to keep a path to the image, and only when you get to the code that displays the image, then you load it, and display it.

So, combining Alexander's and KKRocks on-point answers:

In the top of your class, define your array of filenames:

let imageNames = (0...55).map { "\($0).JPG" }

Where you are displaying your image:

if let image = UIImage(named: images[0]) {
   ... the code that is blowing up ...
}

If you are not sure how to save a file and retrieve it, please see this answer I recently gave on that subject:

Saving and Retrieving Files in User's Space.

Mozahler
  • 4,958
  • 6
  • 36
  • 56
  • Thank you Mozahler for the reply, I have tried adding the code in but have come up wiht an error: " cannot convert value of type 'UIImage' to expected argument type 'String' one the line: 'let image = UIImage(named:images[0]) – diddly101 Jul 12 '17 at 03:12
  • I apologize, my fault. I edited the line in question. I changed the array's name from image to imageNames to make it clear that it contains the filename. – Mozahler Jul 12 '17 at 03:25
  • Thank you for the swift reply, unfortunately i am still having the same issue. the code is currently as follows: let imageNames = (0...50).map{ UIImage(named: "\($0).JPG")!} let image = UIImage(named: imageNames[0]) imageView.animationImages = image imageView.animationDuration = 50 imageView.startAnimating() – diddly101 Jul 12 '17 at 03:33
  • No, that's not right. the map statement shouldn't create images, it should create file names. You want to create an array of names, and only create the image when you want to display it. It's best if you edit your question when you add code. it is hard for everyone to read in the comments, and they might not even read comments. – Mozahler Jul 12 '17 at 03:37
  • Thanks for the comment, modifying the code now to what it is. still having an error though: "cannot assign value of type 'UIImage?' to type '[UIImage]?' on the imageView.animationImages = images – diddly101 Jul 12 '17 at 04:05
  • See the mod I made starting with: if let image = ...{} that will change the UIImage? to UIImage and the error will go away. – Mozahler Jul 18 '17 at 17:13