I am using an UIImage, in which I have an Image, and I want to know the name of image.
-
4i dont think it is possible to get the name of image – The iOSDev Apr 23 '12 at 11:07
-
Answer: UIImageView - How to get the file name of the image assigned? http://stackoverflow.com/a/16885708/2316831?stw=2 – Hasan Sawaed Jun 02 '13 at 18:11
-
See this answer in a related question : https://stackoverflow.com/a/72542728/1356407 – amadour Jun 08 '22 at 14:54
6 Answers
That functionality is not built-in to UIImage
because images are not always loaded from files. However, you could create a custom UIImageView
subclass to fit your needs.

- 54,662
- 15
- 117
- 144
-
-
-
No, this is the correct answer I believe: https://stackoverflow.com/a/72542728/897465 – netdigger Jul 20 '22 at 11:19
It is not possible. UIImage instance contains the actual image data without any reference to the filename.

- 34,169
- 30
- 118
- 167
this code will help you out
NSString *imgName = [self.imgView1st image].accessibilityIdentifier;
NSLog(@"%@",imgName);
[self.imgView2nd setImage:[UIImage imageNamed:imgName]];

- 3,539
- 1
- 27
- 49
-
1this doesn't work because you have to set the accesibiliyIdentifier when assining image – Silviu St Jun 10 '14 at 14:23
-
This is definitely the right answer! People should upvote it. When loading images via local bundle, using XCAssets, the runtime itself will automatically assign the `accessibilityIdentifier` to be the same as the asset name. When loading images dynamically or creating it dynamically, of course there is no built-in solution – user464230 Apr 05 '22 at 16:15
-
The `accessibilityIdentifier` is exposed only when accessibility is enabled on the device. This might be the case that it works for you in development on the simulator, but won't work on actual user's device. – amadour Jun 08 '22 at 14:57
-
This didn't work for me. Neither with simulator or real device. Maybe this requires a specific config / device settings and in this case this answer isn't valid because it is inconsistent and probably not what OP was looking for – RawKnee Sep 15 '22 at 20:41
Images do not necessarily come from files or other named sources, so not all images even have a name. When you create an image from a file, you could store the name in a separate NSString*
, and then refer to that stored name when necessary.

- 714,442
- 84
- 1,110
- 1,523
On later iOS versions it's possible to extract image name from the description. Aware, for the debug use only!
extension UIImage {
/// Extracts image name from a description.
/// * Example description: `<UIImage:0x60000278ce10 named(main: ic_timeline_milestone_bluedot) {16, 16}>`
/// * Example name: `ic_timeline_milestone_bluedot`
/// - warning: For the debug use only.
var name: String? {
let description = self.description
guard let regexp = try? NSRegularExpression(pattern: "\\(main: (.*)\\)", options: []) else { return nil }
guard let match = regexp.matches(in: description, options: [], range: description.fullRange).first else { return nil }
guard match.numberOfRanges > 0 else { return nil }
let idx1 = description.index(description.startIndex, offsetBy: range.lowerBound)
let idx2 = description.index(description.startIndex, offsetBy: range.upperBound)
return String(description[idx1..<idx2])
}
}

- 1,296
- 17
- 17
This answer (https://stackoverflow.com/a/72542728/897465) has (what I believe) the best answer:
let img = UIImage(named: "something")
img?.imageAsset?.value(forKey: "assetName")
Here's a handy extension for it:
extension UIImage {
var containingBundle: Bundle? {
imageAsset?.value(forKey: "containingBundle") as? Bundle
}
var assetName: String? {
imageAsset?.value(forKey: "assetName") as? String
}
}

- 3,659
- 3
- 26
- 49
-
1Asset name as retrieved is a long string eg 2B21CCA7-DE92-4A8C-A877-E17F96F3368E, not the image name as shown in info eg IMG_4304 – Arthur Rees Sep 08 '22 at 03:26