UIImageView – How to get the file name of the image assigned?-Collection of common programming errors


  • Peter Hosey

    Is it possible to read the name of an UIImageView’s image that’s presently stored in the ImageView?

    I was hoping you could do something kind of like this, but haven’t figured it out.

    NSString *currentImageName = [MyIImageView getFileName];
    
  • 11 Answers


  • Jonathan Sterling

    Nope. You can’t do that.

    The reason is that a UIImageView instance does not store an image file. It stores a displays a UIImage instance. When you make an image from a file, you do something like this:

    UIImage *picture = [UIImage imageNamed:@"myFile.png"];
    

    Once this is done, there is no longer any reference to the filename. The UIImage instance contains the data, regardless of where it got it. Thus, the UIImageView couldn’t possibly know the filename.

    Also, even if you could, you would never get filename info from a view. That breaks MVC.


  • j0k

    you can use setAccessibilityIdentifier method for any subclass of UIView

    UIImageView *image ;
    [image setAccessibilityIdentifier:@"file name"] ;
    
    NSString *file_name = [image accessibilityIdentifier] ;
    

  • Kenny Winker

    Nope. No way to do that natively. You’re going to have to subclass UIImageView, and add an imageFileName property (which you set when you set the image).


  • Mihir Mathuria

    Neither UIImageView not UIImage holds on to the filename of the image loaded.

    You can either

    1: (as suggested by Kenny Winker above) subclass UIImageView to have a fileName property or

    2: name the image files with numbers (image1.jpg, image2.jpg etc) and tag those images with the corresponding number (tag=1 for image1.jpg, tag=2 for image2.jpg etc) or

    3: Have a class level variable (eg. NSString *currentFileName) which updates whenever you update the UIImageView’s image


  • Ben Gotow

    No no no. in general these things are possible. It’ll just make you feel like a dirty person. If you absolutely must, do this:

    • Create a category with your own implementation of +imageNamed:(NSString*)imageName that calls through to the existing implementation and uses the technique identified here (How do I use objc_setAssociatedObject/objc_getAssociatedObject inside an object?) to permanently associate imageName with the UIImage object that is returned.

    • Use Method Swizzling to swap the provided implementation of imageNamed: for your implementation in the method lookup table of the Objective-C runtime.

    • Access the name you associated with the UIImage instance (using objc_getAssociatedObject) anytime you want it.

    I can verify that this works, with the caveat that you can’t get the names of UIImage’s loaded in NIBs. It appears that images loaded from NIBs are not created through any standard function calls, so it’s really a mystery to me.

    I’m leaving the implementation up to you. Copy-pasting code that screws with the Objective-C runtime is a very bad idea, so think carefully about your project’s needs and implement this only if you must.


  • sth

    There is no native way to do this; however, you could easily create this behavior yourself.

    You can subclass UIImageView and add a new instance variable:

    NSString* imageFileName;
    

    Then you could override setImage, first setting imageFileName to the filename of the image you’re setting, and then calling [super setImage:imageFileName]. Something like this:

    -(void) setImage:(NSString*)fileName
    {
       imageFileName = fileName;
       [super setImage:fileName];
    }
    

    Just because it can’t be done natively doesn’t mean it isn’t possible 🙂


  • Jay iODroid

    this code will help you out

    - (NSString *) getFileName:(UIImageView *)imgView{
    
        NSString *imgName = [imgView image].accessibilityIdentifier;
    
        NSLog(@"%@",imgName);
    
        return imgName;
    
    }
    

    use this as:

    NSString *currentImageName = [self getFileName:MyIImageView];
    

  • Brent

    You can use objective c Runtime feature for associating imagename with the UImageView.

    First import #import in your class

    then implement your code as below :

    NSString *filename = @"exampleImage";
    UIImage *image = [UIImage imagedName:filename];
    
    objc_setAssociatedObject(image, "imageFilename", filename, OBJC_ASSOCIATION_COPY);
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    
    You can then get the image later:
    
    NSString *filename = objc_getAssociatedObject(imageView, "imageFilename");
    

    Hope it helps you.


  • iEinstein

    if ([imageForCheckMark.image isEqual:[UIImage imageNamed:@"crossCheckMark.png"]]||[imageForCheckMark.image isEqual:[UIImage imageNamed:@"checkMark.png"]])
    {
    
    }
    

  • Hasan W Sawaed

    I have deal with this problem, I have been solved it by MVC design pattern, I created Card class:

    @interface Card : NSObject

    @property (strong,nonatomic) UIImage* img;

    @property (strong,nonatomic) NSString* url;

    @end

    //then in the ViewController in the DidLoad Method to Do :

    // init Cards
    Card* card10= [[Card alloc]init];
    card10.url=@"image.jpg";  
    card10.img = [UIImage imageNamed:[card10 url]];
    

    // for Example

    UIImageView * myImageView = [[UIImageView alloc]initWithImage:card10.img];
    [self.view addSubview:myImageView];
    

    //may you want to check the image name , so you can do this: //for example

     NSString * str = @"image.jpg";
    
     if([str isEqualToString: [card10 url]]){
     // your code here
     }
    

  • ramo

    UIImageView *imageView = ((UIImageView *)(barButtonItem.customView.subviews.lastObject)); file_name = imageView.accessibilityLabel;