{"id":5251,"date":"2014-03-30T20:01:39","date_gmt":"2014-03-30T20:01:39","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/uiimageview-how-to-get-the-file-name-of-the-image-assigned-collection-of-common-programming-errors\/"},"modified":"2014-03-30T20:01:39","modified_gmt":"2014-03-30T20:01:39","slug":"uiimageview-how-to-get-the-file-name-of-the-image-assigned-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/uiimageview-how-to-get-the-file-name-of-the-image-assigned-collection-of-common-programming-errors\/","title":{"rendered":"UIImageView &#8211; How to get the file name of the image assigned?-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/6x6v2.jpg?s=32&amp;g=1\" \/><br \/>\nPeter Hosey<\/p>\n<p>Is it possible to read the name of an UIImageView&#8217;s image that&#8217;s presently stored in the ImageView?<\/p>\n<p>I was hoping you could do something kind of like this, but haven&#8217;t figured it out.<\/p>\n<pre><code>NSString *currentImageName = [MyIImageView getFileName];\n<\/code><\/pre>\n<\/li>\n<li>\n<h3>11 Answers<\/h3>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/4972963194d678c0d284a00159e2ebd3?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJonathan Sterling<\/p>\n<p>Nope. You can&#8217;t do that.<\/p>\n<p>The reason is that a <code>UIImageView<\/code> instance does not store an image file. It stores a displays a <code>UIImage<\/code> instance. When you make an image from a file, you do something like this:<\/p>\n<pre><code>UIImage *picture = [UIImage imageNamed:@\"myFile.png\"];\n<\/code><\/pre>\n<p>Once this is done, there is no longer any reference to the filename. The <code>UIImage<\/code> instance contains the data, regardless of where it got it. Thus, the <code>UIImageView<\/code> couldn&#8217;t possibly know the filename.<\/p>\n<p>Also, even if you could, you would never get filename info from a view. That breaks MVC.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/aee3b44e5f0b4dfa0e2da672897b3751?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nj0k<\/p>\n<p>you can use setAccessibilityIdentifier method for any subclass of UIView<\/p>\n<pre><code>UIImageView *image ;\n[image setAccessibilityIdentifier:@\"file name\"] ;\n\nNSString *file_name = [image accessibilityIdentifier] ;\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/dcd071c0057bee411af9af40e9e03947?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nKenny Winker<\/p>\n<p>Nope. No way to do that natively. You&#8217;re going to have to subclass UIImageView, and add an imageFileName property (which you set when you set the image).<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/3d4fcb6e03c2ff4becf305c6d21b6028?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMihir Mathuria<\/p>\n<p>Neither UIImageView not UIImage holds on to the filename of the image loaded.<\/p>\n<p>You can either<\/p>\n<p>1: (as suggested by Kenny Winker above) subclass UIImageView to have a fileName property or<\/p>\n<p>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<\/p>\n<p>3: Have a class level variable (eg. NSString *currentFileName) which updates whenever you update the UIImageView&#8217;s image<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/ced1555606def805e940f232ac751322?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nBen Gotow<\/p>\n<p>No no no. in general these things are possible. It&#8217;ll just make you feel like a dirty person. If you absolutely must, do this:<\/p>\n<ul>\n<li>\n<p>Create a category with your own implementation of <code>+imageNamed:(NSString*)imageName<\/code> 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 <code>imageName<\/code> with the UIImage object that is returned.<\/p>\n<\/li>\n<li>\n<p>Use Method Swizzling to swap the provided implementation of <code>imageNamed:<\/code> for your implementation in the method lookup table of the Objective-C runtime.<\/p>\n<\/li>\n<li>\n<p>Access the name you associated with the UIImage instance (using objc_getAssociatedObject) anytime you want it.<\/p>\n<\/li>\n<\/ul>\n<p>I can verify that this works, with the caveat that you can&#8217;t get the names of UIImage&#8217;s loaded in NIBs. It appears that images loaded from NIBs are not created through any standard function calls, so it&#8217;s really a mystery to me.<\/p>\n<p>I&#8217;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&#8217;s needs and implement this only if you must.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/92fb4563ddc5ceeaa8b19b60a7a172f4?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nsth<\/p>\n<p>There is no native way to do this; however, you could easily create this behavior yourself.<\/p>\n<p>You can subclass <code>UIImageView<\/code> and add a new instance variable:<\/p>\n<pre><code>NSString* imageFileName;\n<\/code><\/pre>\n<p>Then you could override <code>setImage<\/code>, first setting <code>imageFileName<\/code> to the filename of the image you&#8217;re setting, and then calling <code>[super setImage:imageFileName]<\/code>. Something like this:<\/p>\n<pre><code>-(void) setImage:(NSString*)fileName\n{\n   imageFileName = fileName;\n   [super setImage:fileName];\n}\n<\/code><\/pre>\n<p>Just because it can&#8217;t be done natively doesn&#8217;t mean it isn&#8217;t possible \ud83d\ude42<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/40WsQ.png?s=32&amp;g=1\" \/><br \/>\nJay iODroid<\/p>\n<p>this code will help you out<\/p>\n<pre><code>- (NSString *) getFileName:(UIImageView *)imgView{\n\n    NSString *imgName = [imgView image].accessibilityIdentifier;\n\n    NSLog(@\"%@\",imgName);\n\n    return imgName;\n\n}\n<\/code><\/pre>\n<p>use this as:<\/p>\n<pre><code>NSString *currentImageName = [self getFileName:MyIImageView];\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/bccc8454971169a0c956841fcebcc907?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nBrent<\/p>\n<p><strong>You can use objective c Runtime feature for associating imagename with the UImageView.<\/strong><\/p>\n<p>First import <code>#import<\/code> in your class<\/p>\n<p>then implement your code as below :<\/p>\n<pre><code>NSString *filename = @\"exampleImage\";\nUIImage *image = [UIImage imagedName:filename];\n\nobjc_setAssociatedObject(image, \"imageFilename\", filename, OBJC_ASSOCIATION_COPY);\n\nUIImageView *imageView = [[UIImageView alloc] initWithImage:image];\n\nYou can then get the image later:\n\nNSString *filename = objc_getAssociatedObject(imageView, \"imageFilename\");\n<\/code><\/pre>\n<p>Hope it helps you.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/5U5WP.jpg?s=32&amp;g=1\" \/><br \/>\niEinstein<\/p>\n<pre><code>if ([imageForCheckMark.image isEqual:[UIImage imageNamed:@\"crossCheckMark.png\"]]||[imageForCheckMark.image isEqual:[UIImage imageNamed:@\"checkMark.png\"]])\n{\n\n}\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/1SG5r.jpg?s=32&amp;g=1\" \/><br \/>\nHasan W Sawaed<\/p>\n<p>I have deal with this problem, I have been solved it by MVC design pattern, I created Card class:<\/p>\n<p>@interface Card : NSObject<\/p>\n<p>@property (strong,nonatomic) UIImage* img;<\/p>\n<p>@property (strong,nonatomic) NSString* url;<\/p>\n<p>@end<\/p>\n<p>\/\/then in the ViewController in the DidLoad Method to Do :<\/p>\n<pre><code>\/\/ init Cards\nCard* card10= [[Card alloc]init];\ncard10.url=@\"image.jpg\";  \ncard10.img = [UIImage imageNamed:[card10 url]];\n<\/code><\/pre>\n<p>\/\/ for Example<\/p>\n<pre><code>UIImageView * myImageView = [[UIImageView alloc]initWithImage:card10.img];\n[self.view addSubview:myImageView];\n<\/code><\/pre>\n<p>\/\/may you want to check the image name , so you can do this: \/\/for example<\/p>\n<pre><code> NSString * str = @\"image.jpg\";\n\n if([str isEqualToString: [card10 url]]){\n \/\/ your code here\n }\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/be1e4643e79fb82f4b6297ba6bcddeae?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nramo<\/p>\n<p>UIImageView *imageView = ((UIImageView *)(barButtonItem.customView.subviews.lastObject)); file_name = imageView.accessibilityLabel;<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Peter Hosey Is it possible to read the name of an UIImageView&#8217;s image that&#8217;s presently stored in the ImageView? I was hoping you could do something kind of like this, but haven&#8217;t figured it out. NSString *currentImageName = [MyIImageView getFileName]; 11 Answers Jonathan Sterling Nope. You can&#8217;t do that. The reason is that a UIImageView [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5251","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5251"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5251\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}