ios UIImageView - 如何获取分配的图像的文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1740274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIImageView - How to get the file name of the image assigned?
提问by Kuberchaun
Is it possible to read the name of an UIImageView's
UIImage
that's presently stored in the UIImageView
?
是否可以读取UIImageView's
UIImage
当前存储在UIImageView
?
I was hoping you could do something kind of like this, but haven't figured it out.
我希望你能做这样的事情,但还没有弄清楚。
NSString *currentImageName = [MyIImageView getFileName];
回答by Nizar Ahmed
you can use setAccessibilityIdentifier method for any subclass of UIView
您可以对 UIView 的任何子类使用 setAccessibilityIdentifier 方法
UIImageView *image ;
[image setAccessibilityIdentifier:@"file name"] ;
NSString *file_name = [image accessibilityIdentifier] ;
回答by 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:
原因是UIImageView
实例不存储图像文件。它存储一个显示UIImage
实例。当您从文件制作图像时,您会执行以下操作:
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.
完成此操作后,不再有对文件名的任何引用。该UIImage
实例包含的数据,无论它得到了它。因此,UIImageView
不可能知道文件名。
Also, even if you could, you would never get filename info from a view. That breaks MVC.
此外,即使可以,您也永远不会从视图中获取文件名信息。这打破了MVC。
回答by 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 associateimageName
with the UIImage object that is returned.Use Method Swizzlingto 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.
使用您自己的实现
+imageNamed:(NSString*)imageName
调用到现有实现创建一个类别,并使用此处标识的技术(如何在对象内使用 objc_setAssociatedObject/objc_getAssociatedObject?)imageName
与返回的 UIImage 对象永久关联。使用Method Swizzling
imageNamed:
在 Objective-C 运行时的方法查找表中为您的实现交换提供的实现。随时访问与 UIImage 实例关联的名称(使用 objc_getAssociatedObject)。
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.
我可以验证这是否有效,但需要注意的是,您无法获取 NIB 中加载的 UIImage 的名称。从 NIB 加载的图像似乎不是通过任何标准函数调用创建的,所以这对我来说真的是个谜。
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.
我把实现留给你。复制粘贴与 Objective-C 运行时有关的代码是一个非常糟糕的主意,因此请仔细考虑您的项目需求,并仅在必须时才实施。
回答by iEinstein
if ([imageForCheckMark.image isEqual:[UIImage imageNamed:@"crossCheckMark.png"]]||[imageForCheckMark.image isEqual:[UIImage imageNamed:@"checkMark.png"]])
{
}
回答by WendiKidd
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:
您可以创建子类UIImageView
并添加一个新的实例变量:
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:
然后你可以覆盖setImage
,首先设置imageFileName
为你正在设置的图像的文件名,然后调用[super setImage:imageFileName]
. 像这样的东西:
-(void) setImage:(NSString*)fileName
{
imageFileName = fileName;
[super setImage:fileName];
}
Just because it can't be done natively doesn't mean it isn't possible :)
仅仅因为它不能在本地完成并不意味着它不可能:)
回答by 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).
不。没有办法在本地做到这一点。您将不得不继承 UIImageView,并添加一个 imageFileName 属性(您在设置图像时设置)。
回答by Mihir Mathuria
Neither UIImageView not UIImage holds on to the filename of the image loaded.
UIImageView 和 UIImage 都不保留加载图像的文件名。
You can either
你可以
1: (as suggested by Kenny Winker above) subclass UIImageView to have a fileName property or
1:(如上面 Kenny Winker 所建议的)子类 UIImageView 具有 fileName 属性或
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
2:用数字命名图像文件(image1.jpg,image2.jpg等)并用相应的数字标记这些图像(image1.jpg的tag=1,image2.jpg的tag=2等)或
3: Have a class level variable (eg. NSString *currentFileName) which updates whenever you update the UIImageView's image
3:有一个类级变量(例如 NSString *currentFileName),它会在你更新 UIImageView 的图像时更新
回答by junaidsidhu
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];
回答by ThiagoAM
Or you can use the restoration identifier, like this:
或者您可以使用恢复标识符,如下所示:
let myImageView = UIImageView()
myImageView.image = UIImage(named: "anyImage")
myImageView.restorationIdentifier = "anyImage" // Same name as image's name!
// Later, in UI Tests:
print(myImageView.restorationIdentifier!) // Prints "anyImage"
Basically in this solution you're using the restoration identifier to hold the image's name, so you can use it later anywhere. If you update the image, you must also update the restoration identifier, like this:
基本上在这个解决方案中,您使用恢复标识符来保存图像的名称,以便您以后可以在任何地方使用它。如果更新映像,则还必须更新恢复标识符,如下所示:
myImageView.restorationIdentifier = "newImageName"
I hope that helps you, good luck!
希望能帮到你,祝你好运!
回答by Nishant Tyagi
You can use objective c Runtime feature for associating imagename with the UImageView.
您可以使用目标 c 运行时功能将 imagename 与 UImageView 相关联。
First import #import <objc/runtime.h>
in your class
#import <objc/runtime.h>
在您的班级中首次导入
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.
希望对你有帮助。