Eager Beaver
Eager Beaver

Reputation: 979

How to get Image Name used in an UIImage?

I am using an UIImage, in which I have an Image, and I want to know the name of image.

Upvotes: 21

Views: 43541

Answers (6)

Anton Plebanovich
Anton Plebanovich

Reputation: 1516

On later iOS versions it's possible to extract image name from the description. Aware, for the debug use only!

extension StringProtocol {
    
    /// Range from the start to the end.
    var fullNSRange: NSRange {
        NSRange(location: 0, length: count)
    }
}

extension UIImage {
    
    /// Extracts image name from a description.
    ///
    /// * Example description 1: `<UIImage:0x600003005320 named(IMG_6312.heic) {4284, 5712} renderingMode=automatic(original)>`
    /// * Example name 1: `IMG_6312.heic`
    ///
    /// * Example description 2: `<UIImage:0x60000278ce10 named(main: ic_timeline_milestone_bluedot) {16, 16}>`
    /// * Example name 2: `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.fullNSRange).first else { return nil }
        guard match.numberOfRanges > 0 else { return nil }
        let range = match.range(at: match.numberOfRanges - 1)
        let idx1 = description.index(description.startIndex, offsetBy: range.lowerBound)
        let idx2 = description.index(description.startIndex, offsetBy: range.upperBound)
        return String(description[idx1..<idx2])
    }
}

Upvotes: 1

netdigger
netdigger

Reputation: 3789

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
    }

}

Upvotes: 1

junaidsidhu
junaidsidhu

Reputation: 3580

this code will help you out

    NSString *imgName = [self.imgView1st image].accessibilityIdentifier;

    NSLog(@"%@",imgName);

    [self.imgView2nd setImage:[UIImage imageNamed:imgName]];

Upvotes: 4

Krishnabhadra
Krishnabhadra

Reputation: 34265

It is not possible. UIImage instance contains the actual image data without any reference to the filename.

Upvotes: 5

Evan Mulawski
Evan Mulawski

Reputation: 55334

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.

Upvotes: 8

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

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.

Upvotes: 1

Related Questions