Objective c 比较图像背景
背景图像已经在Xcode项目中设置好了,但是当点击按钮时它会改变,所以我想检查背景图像Objective c 比较图像背景,objective-c,xcode,image,compare,Objective C,Xcode,Image,Compare,背景图像已经在Xcode项目中设置好了,但是当点击按钮时它会改变,所以我想检查背景图像80red.png是否与刚才点击的按钮的背景相同 -(IBAction)buttonChange:(id)sender { redImage = [UIImage imageNamed:@"80red.png”]; UIImage *ButtonColour = [sender backgroundImageForState:UIControlStateNormal];
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没有链接到对象UIImage类-您必须编写
UIImage*redImage=[UIImage ImageName:@“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;
}
-(iAction)按钮更改:(id)发件人{
}
我希望我的建议能对你有所帮助
另外,很抱歉我的英语=)@user2643679如果我的建议对你有帮助,请喜欢它:)
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");
}