Swift: How to extract image filename from an array of UIImage

╄→尐↘猪︶ㄣ 提交于 2019-12-23 15:22:38

问题


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?


回答1:


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!




回答2:


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.




回答3:


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) }



回答4:


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)

        }


来源:https://stackoverflow.com/questions/29113598/swift-how-to-extract-image-filename-from-an-array-of-uiimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!