Finding image name loaded into UIImage instance

爱⌒轻易说出口 提交于 2019-12-24 19:27:04

问题


How can I find out what the image name (usually file name?) loaded into a UIImage instance is? In this case, they were all initWithContentsOfFile:.


回答1:


Once it's loaded using that, you can't. The file name is only used to load the data into memory. After that, it becomes irrelevant.

If you need to maintain the file name, then perhaps you need to make a class with the filename (as an NSString or NSURL, perhaps) and the image data (as an NSImage or NSData object) as ivars. I'm not sure what your application is, so I can't give you any architectural advice at this point.




回答2:


If you already know what the image names are you can compare your image to another image using image named:

if (bg.image == [UIImage imageNamed:@"field.jpeg"]) {
    bg.image = [UIImage imageNamed:@"cave.jpeg"];
} else {
    bg.image = [UIImage imageNamed:@"field.jpeg"];
}

Here I use one button to switch between two images.




回答3:


It's also something you can't just add a category on UIImage to, unfortunately, because it requires state.



来源:https://stackoverflow.com/questions/1523322/finding-image-name-loaded-into-uiimage-instance

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