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

问题描述 投票:75回答:16

是否有可能读取目前存储在UIImageView's中的UIImage UIImageView的名称?

我希望你能做一些像这样的事,但还没弄明白。

NSString *currentImageName = [MyIImageView getFileName];
ios objective-c swift uiimageview uiimage
16个回答
83
投票

不。你不能这样做。

原因是UIImageView实例不存储图像文件。它存储一个显示UIImage实例。从文件制作图像时,您可以执行以下操作:

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

完成此操作后,不再引用文件名。 UIImage实例包含数据,无论它在何处获得。因此,UIImageView不可能知道文件名。

此外,即使你可以,你也永远不会从视图中获取文件名信息。这打破了MVC。


2
投票

您可以使用objective c Runtime功能将图像名称与UIimageView相关联。

首先在你的班级导入#import <objc/runtime.h>

然后实现您的代码如下:

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");

希望它能帮到你。


2
投票

或者您可以使用恢复标识符,如下所示:

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"

基本上在此解决方案中,您使用恢复标识符来保存图像的名称,以便您可以在以后的任何地方使用它。如果更新映像,则还必须更新还原标识符,如下所示:

myImageView.restorationIdentifier = "newImageName"

我希望能帮到你,祝你好运!


1
投票

斯威夫特3

首先将accessibilityIdentifier设置为imageName

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

然后使用以下代码。

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

最后,确定方法的调用方式

myImageView.getFileName()

1
投票

获取图像名称Swift 4.2

如果要比较资产中的按钮图像名称,有一种方法。

@IBOutlet weak var extraShotCheckbox: UIButton!

@IBAction func extraShotCheckBoxAction(_ sender: Any) {
    extraShotCheckbox.setImage(changeCheckBoxImage(button: extraShotCheckbox), for: .normal)
}

func changeCheckBoxImage(button: UIButton) -> UIImage {
    if let imageView = button.imageView, let image = imageView.image {
        if image == UIImage(named: "checkboxGrayOn") {
            return UIImage(named: "checkbox")!
        } else {
            return UIImage(named: "checkboxGrayOn")!
        }
    }
    return UIImage()
}

0
投票

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

@interface Card : NSObject

@property (strong,nonatomic) UIImage* img;

@property  (strong,nonatomic) NSString* url;

@end

//然后在UIViewController方法中的DidLoad

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

// 例如

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

//你想查看图片名称吗,这样你就可以这样做:

//例如

 NSString * str = @"image.jpg";

 if([str isEqualToString: [card10 url]]){
 // your code here
 }

-1
投票

使用下面

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

-1
投票

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

if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? 

NS RL {

  ALAssetsLibrary().asset(for: referenceUrl as URL!, resultBlock: { asset in

    let fileName = asset?.defaultRepresentation().filename()
    print(fileName!)

    //do whatever with your file name            
    }, failureBlock: nil)
}

101
投票

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

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

NSString *file_name = [image accessibilityIdentifier] ;

18
投票

不,不,一般来说,这些都是可能的。它会让你觉得自己像个肮脏的人。如果绝对必须,请执行以下操作:

我可以验证这是否有效,但需要注意的是你无法获得在NIB中加载的UIImage的名称。看起来从NIB加载的图像不是通过任何标准函数调用创建的,所以对我来说这真的很神秘。

我要离开你的实施。使用Objective-C运行时的复制粘贴代码是一个非常糟糕的主意,因此请仔细考虑项目的需求,并在必要时实现此目的。


10
投票

没有本地方法可以做到这一点;但是,您可以自己轻松创建此行为。

您可以继承UIImageView并添加新的实例变量:

NSString* imageFileName;

然后你可以覆盖setImage,首先将imageFileName设置为你正在设置的图像的文件名,然后调用[super setImage:imageFileName]。像这样的东西:

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

仅仅因为它本身无法完成并不意味着它是不可能的:)


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

}

9
投票

不。没办法原生那样做。您将不得不子类化UIImageView,并添加一个imageFileName属性(您在设置图像时设置)。


7
投票

UIImageView和UIImage都不会保留加载的图像的文件名。

你也可以

1 :(如上面的Kenny Winker所建议的)子类UIImageView具有fileName属性或

2:使用数字(image1.jpg,image2.jpg等)命名图像文件,并用相应的数字标记这些图像(image1.jpg为tag = 1,image2.jpg等为tag = 2)或

3:有一个类级变量(例如NSString * currentFileName),每当你更新UIImageView的图像时它都会更新


3
投票

此代码将帮助您: -

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

用它作为: -

NSString *currentImageName = [self getFileName:MyIImageView];

2
投票

是的,您可以与下面代码等数据的帮助进行比较

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.