UIImageView -如何获得分配的图像的文件名?

时间:2021-09-22 17:17:07

Is it possible to read the name of an UIImageView's UIImage that's presently stored in the UIImageView?

是否可以读取当前存储在UIImageView中的UIImageView的UIImage的名称?

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

我希望你能做这样的事,但还没想明白。

NSString *currentImageName = [MyIImageView getFileName];

15 个解决方案

#1


81  

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。

#2


93  

you can use setAccessibilityIdentifier method for any subclass of UIView

您可以对UIView的任何子类使用setAccessibilityIdentifier方法

UIImageView *image ;
[image setAccessibilityIdentifier:@"file name"] ;

NSString *file_name = [image accessibilityIdentifier] ;

#3


14  

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.

    使用您自己的+imageNamed:(NSString*)imageName实现创建一个类别,该实现通过现有实现调用并使用这里标识的技术(我如何使用objc_setAssociatedObject/objc_getAssociatedObject)在对象中永久地将imageName与返回的UIImage对象关联?

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

    使用方法swizzle交换提供的imageNamed:在Objective-C运行时的方法查找表中的实现。

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

    随时访问与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.

我可以验证它是否有效,但有一点要注意,您无法在NIBs中加载UIImage的名称。从NIBs加载的图像似乎不是通过任何标准的函数调用创建的,所以对我来说这真的是一个谜。

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运行时进行复制粘贴代码是一个非常糟糕的想法,所以要仔细考虑项目的需求,并在必须的情况下实现它。

#4


10  

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 :)

仅仅因为它不能在本地完成,并不意味着它不可能完成:

#5


9  

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

}

#6


8  

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属性(在设置图像时设置该属性)。

#7


7  

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具有文件名属性或

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标记为1,image2.jpg标记为2)或

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

3:有一个类级变量(如。NSString *currentFileName)当你更新UIImageView的图像时更新的

#8


3  

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

#9


2  

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

您可以使用objective c运行时特性将imagename与UImageView关联起来。

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

第一次进口#进口< objc /运行时。在你的课上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.

希望它能帮助你。

#10


1  

Yes you can compare with the help of data like 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;
}

#11


1  

Swift 3

斯威夫特3

First set the accessibilityIdentifier as imageName

首先将accessibilityIdentifier设置为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, The calling way of method to identify

最后,对调用方法进行识别

myImageView.getFileName()

#12


1  

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!

我希望这能帮助你,祝你好运!

#13


0  

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

我已经解决了这个问题,我已经通过MVC设计模式解决了它,我创建了Card类:

@interface Card : NSObject

@property (strong,nonatomic) UIImage* img;

@property  (strong,nonatomic) NSString* url;

@end

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

//然后在UIViewController DidLoad方法中做:

// 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
 }

#14


-1  

use below

使用下面的

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

#15


-1  

The code is work in swift3 - write code inside didFinishPickingMediaWithInfo delegate method:

代码在swift3中工作——在didFinishPickingMediaWithInfo委托方法中编写代码:

if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? 

NSURL {

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)
}

#1


81  

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。

#2


93  

you can use setAccessibilityIdentifier method for any subclass of UIView

您可以对UIView的任何子类使用setAccessibilityIdentifier方法

UIImageView *image ;
[image setAccessibilityIdentifier:@"file name"] ;

NSString *file_name = [image accessibilityIdentifier] ;

#3


14  

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.

    使用您自己的+imageNamed:(NSString*)imageName实现创建一个类别,该实现通过现有实现调用并使用这里标识的技术(我如何使用objc_setAssociatedObject/objc_getAssociatedObject)在对象中永久地将imageName与返回的UIImage对象关联?

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

    使用方法swizzle交换提供的imageNamed:在Objective-C运行时的方法查找表中的实现。

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

    随时访问与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.

我可以验证它是否有效,但有一点要注意,您无法在NIBs中加载UIImage的名称。从NIBs加载的图像似乎不是通过任何标准的函数调用创建的,所以对我来说这真的是一个谜。

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运行时进行复制粘贴代码是一个非常糟糕的想法,所以要仔细考虑项目的需求,并在必须的情况下实现它。

#4


10  

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 :)

仅仅因为它不能在本地完成,并不意味着它不可能完成:

#5


9  

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

}

#6


8  

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属性(在设置图像时设置该属性)。

#7


7  

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具有文件名属性或

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标记为1,image2.jpg标记为2)或

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

3:有一个类级变量(如。NSString *currentFileName)当你更新UIImageView的图像时更新的

#8


3  

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

#9


2  

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

您可以使用objective c运行时特性将imagename与UImageView关联起来。

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

第一次进口#进口< objc /运行时。在你的课上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.

希望它能帮助你。

#10


1  

Yes you can compare with the help of data like 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;
}

#11


1  

Swift 3

斯威夫特3

First set the accessibilityIdentifier as imageName

首先将accessibilityIdentifier设置为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, The calling way of method to identify

最后,对调用方法进行识别

myImageView.getFileName()

#12


1  

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!

我希望这能帮助你,祝你好运!

#13


0  

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

我已经解决了这个问题,我已经通过MVC设计模式解决了它,我创建了Card类:

@interface Card : NSObject

@property (strong,nonatomic) UIImage* img;

@property  (strong,nonatomic) NSString* url;

@end

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

//然后在UIViewController DidLoad方法中做:

// 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
 }

#14


-1  

use below

使用下面的

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

#15


-1  

The code is work in swift3 - write code inside didFinishPickingMediaWithInfo delegate method:

代码在swift3中工作——在didFinishPickingMediaWithInfo委托方法中编写代码:

if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? 

NSURL {

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)
}