比较图像背景

Compare image background

背景图已经在Xcode项目中设置了,但是当点击按钮时它发生了变化,所以我想检查背景图80red.png是否与按钮的背景刚刚被点击。

-(IBAction)buttonChange:(id)sender {

    redImage = [UIImage imageNamed:@"80red.png”];

    UIImage *ButtonColour = [sender backgroundImageForState:UIControlStateNormal];       

    NSData *ButtonColourData = UIImagePNGRepresentation(ButtonColour);
    NSData *redImageData = UIImagePNGRepresentation(redImage);

    if ([ButtonColourData isEqual: redImageData]) {

        // if images are the same
        NSLog(@"Images are the same");    
    }    
}

我不明白为什么我的代码不起作用。

您的代码无法运行,因为您的代码语法有误。

redImage 不是 link 对象 UIImage class - 你必须写 UIImage *redImage = [UIImage imageNamed:@"80red.png"];

下一个:

当你比较图像的日期时,你比较的是图像的大小,但不同的图像可能大小相同。更好地比较图像的内容。

我假装了你的情况,这就是我得到的:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.


UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 2, 2)];
[button setBackgroundImage:[UIImage imageNamed:@"80red.png"] forState:UIControlStateNormal];

[self buttonChange:button];

return YES;

}

-(IBAction)buttonChange:(id)发件人{

UIImage *redImage = [UIImage imageNamed:@"80red.png"];

UIImage *ButtonColour = [sender backgroundImageForState:UIControlStateNormal];

//NSData *ButtonColourData = UIImagePNGRepresentation(ButtonColour);
//NSData *redImageData = UIImagePNGRepresentation(redImage);

if ([redImage isEqual: ButtonColour]) {

    // if images are the same
    NSLog(@"Images are the same");

}

}

希望我的建议对您有所帮助。

P.S。对不起我的英语=)