How to compare two UIImage in iOS 8 [duplicate]

女生的网名这么多〃 提交于 2019-12-10 10:58:32

问题


I am using the method given below

IBOutlet UIImageView *imgViewCheck;

- (IBAction)clkCheckUncheck:(id)sender {
    if (imgViewCheck.image == [UIImage imageNamed:@"uncheck.png"]) {
        imgViewCheck.image = [UIImage imageNamed:@"check.png"];
    } else {
        imgViewCheck.image = [UIImage imageNamed:@"uncheck.png"];
    }
}

It is working in up to iOS7 but not working in iOS8


回答1:


Please compare NSData object instead of comparing UIImage Object.

-(BOOL)firstimage:(UIImage *)image1 isEqualTo:(UIImage *)image2 {
    NSData *data1 = UIImagePNGRepresentation(image1);
    NSData *data2 = UIImagePNGRepresentation(image2);

    return [data1 isEqualToData:data2];
}



回答2:


I'd not rely on comparing image property with address returned by UIImage method (it could be newly allocated block I suppose).

If you really need to compare if data(image) is same you could use something like:

UIImage* checkImage = [UIImage imageNamed:@"check.png"];
NSData *checkImageData = UIImagePNGRepresentation(checkImage);
NSData *propertyImageData = UIImagePNGRepresentation(imgViewCheck.image);
if ([checkImageData isEqualToData:propertyImageData]) {
  //do sth
}

where possible I'd prefer to keep state/imageId in a variable for comparison purposes



来源:https://stackoverflow.com/questions/26467185/how-to-compare-two-uiimage-in-ios-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!