UIImageView - How to get file name for image?

Can I read the name of UIImageView's UIImage which is currently stored in UIImageView ?

I was hoping you could do something like this, but didn't understand it.

 NSString *currentImageName = [MyIImageView getFileName]; 
+77
ios objective-c uikit cocoa swift uiimage uiimageview
Nov 16 '09 at 5:36
source share
15 answers

Nope. You cannot do this.

The reason is that the UIImageView instance UIImageView not store the image file. It stores an instance of UIImage . When you make an image from a file, you are doing something like this:

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

Once this is done, there is no longer a link to the file name. A UIImage instance contains data, regardless of where it is received. Therefore, UIImageView cannot know the file name.

Also, even if you could, you would never get file name information from the view. This destroys MVC.

+85
Nov 16 '09 at 5:39
source share

you can use setAccessibilityIdentifier method for any subclass of UIView

 UIImageView *image ; [image setAccessibilityIdentifier:@"file name"] ; NSString *file_name = [image accessibilityIdentifier] ; 
+103
Sep 04 '12 at 11:19
source share

No, no ... in general, anything is possible. It just makes you feel like a dirty person. If you absolutely need to, do the following:

  • Create a category with your own implementation +imageNamed:(NSString*)imageName , which refers to the existing implementation and uses the technique described here ( How to use the objc_setAssociatedObject / objc_getAssociatedObject inside the object? ) To permanently associate imageName with the returned UIImage.

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

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

I can verify that this works, with the caveat that you cannot get the UIImage names loaded in the NIB. It seems that images loaded from NIB are not created by any standard function calls, so for me this is really a mystery.

I leave the implementation to you. The coding code that clamps using the Objective-C framework is a very bad idea, so keep a close eye on the needs of the project and implement this only if you must.

+19
Dec 24 '12 at 6:18
source share

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

You can subclass UIImageView and add a new instance variable:

 NSString* imageFileName; 

Then you can override setImage , first set imageFileName to the name of the image file that you are setting, and then call [super setImage:imageFileName] . Something like that:

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

Just because it is impossible to do it from the beginning does not mean that it is impossible :)

+10
May 22 '12 at 1:26
source share
 if ([imageForCheckMark.image isEqual:[UIImage imageNamed:@"crossCheckMark.png"]]||[imageForCheckMark.image isEqual:[UIImage imageNamed:@"checkMark.png"]]) { } 
+10
Feb 19 '13 at 11:03
source share

Nope. It is impossible to do this initially. You will have to subclass UIImageView and add the imageFileName property (which you set when adjusting the image).

+9
Nov 16 '09 at 5:37
source share

Neither UIImageView nor UIImage are bound to the file name of the loaded image.

You can either

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

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

3: Have a class level variable (e.g. NSString * currentFileName) that is updated whenever you update the UIImageView image

+7
Dec 17 '09 at 22:27
source share

This code will help you: -

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

Use it as: -

 NSString *currentImageName = [self getFileName:MyIImageView]; 
+3
May 29 '13 at
source share

You can use the lens function with Runtime to map the image name to a UImageView.

First import of #import <objc/runtime.h> in your class

then execute 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 this helps you.

+2
Dec 20 '13 at 9:32
source share

Yes, you can compare using data such as below code

 UITableViewCell *cell = (UITableViewCell*)[self.view viewWithTag:indexPath.row + 100]; UIImage *secondImage = [UIImage imageNamed:@"boxhover.png"]; NSData *imgData1 = UIImagePNGRepresentation(cell.imageView.image); NSData *imgData2 = UIImagePNGRepresentation(secondImage); BOOL isCompare = [imgData1 isEqual:imgData2]; if(isCompare) { //contain same image cell.imageView.image = [UIImage imageNamed:@"box.png"]; } else { //does not contain same image cell.imageView.image = secondImage; } 
+2
Oct 13 '14 at 7:01
source share

Or you can use the recovery identifier, for example:

 let myImageView = UIImageView() myImageView.image = UIImage(named: "anyImage") myImageView.restorationIdentifier = "anyImage" // Same name as image name! // Later, in UI Tests: print(myImageView.restorationIdentifier!) // Prints "anyImage" 

Basically in this solution, you use a recovery identifier to store the image name so that it can be used later anywhere. If you update the image, you must also update the recovery identifier, for example:

 myImageView.restorationIdentifier = "newImageName" 

I hope this helps, good luck!

+2
May 23 '17 at 11:03 a.m.
source share

Swift 3

First set the accessibilityIdentifier as imageName

 myImageView.image?.accessibilityIdentifier = "add-image" 

Then use the following code.

 extension UIImageView { func getFileName() -> String? { // First set accessibilityIdentifier of image before calling. let imgName = self.image?.accessibilityIdentifier return imgName } } 

Finally, a method call method to determine

 myImageView.getFileName() 
+1
Apr 13 '17 at 10:29 on
source share

I have a problem with this problem, I solved it using the MVC design pattern, I created a map class:

 @interface Card : NSObject @property (strong,nonatomic) UIImage* img; @property (strong,nonatomic) NSString* url; @end 

// then in the UIViewController in the DidLoad method:

 // 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]; 

// you can check the image name so you can do this:

//eg

  NSString * str = @"image.jpg"; if([str isEqualToString: [card10 url]]){ // your code here } 
0
Jun 02 '13 at 18:07
source share

use below

  UIImageView *imageView = ((UIImageView *)(barButtonItem.customView.subviews.lastObject)); file_name = imageView.accessibilityLabel; 
-one
Dec 05 '13 at 1:18
source share

The code works in swift3 - it writes code inside the didFinishPickingMediaWithInfo method:

 if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? 

NSURL {

  ALAssetsLibrary().asset(for: referenceUrl as URL!, resultBlock: { asset in let fileName = asset?.defaultRepresentation().filename() print(fileName!) //do whatever with your file name }, failureBlock: nil) } 
-one
Nov 27 '17 at 10:00
source share



All Articles