4

If I have an array of UIImage like so:

newImageArray = [UIImage(named:"Red.png")!,
        UIImage(named:"Green.png")!,
        UIImage(named:"Blue.png")!, UIImage(named:"Yellow.png")!]

How can I extract or determine the filename of an image of a certain index later on? For example:

println("The first image is \(newImageArray[0])")

Instead of returning a readable filename, it returns:

The first image is <UIImage: 0x7fe211d2b1a0>

Can I convert this output into readable text, or is there a different method of extracting filenames from UIImage arrays?

2
  • 2
    Maybe you should keep track of what name you used to create the image. Commented Mar 18, 2015 at 3:26
  • Looks like it might be the way to go. Thanks for the suggestion. Commented Mar 18, 2015 at 6:07

4 Answers 4

4

I have had a look around as this has been a problem for me in the past also. UIImage does not store the filename of the image. What I did to solve the issue was instead of an image array I used a dictionary with the key as the filename and the value as the Image. In my for loop I extracted the key and value of each item into a tuple and dealt with them.

I no longer have the code but as a quick mock up of what I did see below, (I hope this fits your requirements as I know every application is different)

var imageDictionary = ["image1.png": UIImage(named: "image1.png"),
     "image2.png": UIImage(named: "image2.png")]

and then the for loop will look like:

for (key, value) in imageDictionary {
    println(key) // Deal with Key
    println(value) // Deal with Value
}

...as I say this worked for me and the scenario I needed it for, I hope you can use it also!

Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

That would be a really neat way to do it. So what would be the syntax to then check if an index is a specific filename (key)?
var index = imageDictionary.indexForKey("Key")
No probs, swift dictionary has all kinds of methods. Hopefully one will suit.
1

Once you create the instance of the UIImage, all references to the name are lost. For example, when you make an image from a file, you do something like this:

var image: UIImage = UIImage(named: "image.png")

After that's done, there are no more references to the file name. All of the data is stored in the UIImage instance, regardless of where it came from. As the comments said above, you'll need to devise a way to store the names if you must do so.

1 Comment

Thanks for the confirmation on that. I'll have to do some extra coding before creating the array.
0

My approach would be to start with an array of the names (since you can't go back to the names from the images easily):

let imageNames = [ "Red.png", "Green.png", "Blue.png", "Yellow.png"]

Then you can create an array of the images using:

let images = imageNames.map { UIImage(named: $0) }

2 Comments

You forgot to unwrap the optional returned by UIImage(named:$0)!
Actually, I didn't forget, it was a conscious choice :) Either way works, but I'm really not a fan of hard unwrapping optionals and this lets you handle it in a more user friendly manner.
0

Its a really tricky one. I finally managed to get it to work within a Table View Controller. ( Swift 2.1, Xcode 7)

My Image names were as below in the 'icons' array with .png extension. I created an array for the image names.

        var icons = ["BirthdaysImage", "AppointmentsImage", "GroceriesImage","MiscellaneousImage"]

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("iconsReuseIdentifier", forIndexPath: indexPath)

   //image display
            iconName = icons[indexPath.row]
            cell.imageView?.image = UIImage(named: iconName)

            return cell
        }

     override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

   //find out which IMAGE got selected
            var imageSelected = icons[indexPath.row]
            print(imageSelected)

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.