UIImageView 设置imagename后,获取fileName的方法

UIImageView的实例不会保存一个image file,它显示的是一个UIImage实例。当给UIImageView设置imageNamed后,无法再从UIImageView实例获取fileName。
1.从UIImageView继承一个subclass,设置一个NSString * imageFileName;属性
和setImage方法:

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

2.使用accessibilityIdntifier

通过下面setAccessibilityIdentifier
[_checkImageView.image setAccessibilityIdentifier:@"yixuan"]; 
NSString *file_name = [_checkImageView.image accessibilityIdentifier] ;  
// 判断当前是已选还是未选中状态
if ([_checkImageView.image.accessibilityIdentifier isEqualToString:@"yixuan"]) {
        _checkImageView.image = [UIImage imageNamed:@"weixuan"];
        _checkImageView.image.accessibilityIdentifier = @"weixuan"; 
    }else {
        _checkImageView.image = [UIImage imageNamed:@"yixuan"];
        _checkImageView.image.accessibilityIdentifier = @"yixuan";
    }

你可能感兴趣的:(UIImageView 设置imagename后,获取fileName的方法)