gpt4 book ai didi

ios - 为什么与 UIImagePNGRepresentation 的比较不起作用?

转载 作者:行者123 更新时间:2023-11-29 02:11:40 26 4
gpt4 key购买 nike

我有一个应该代表复选框的按钮。单击按钮时,imageView.image-Property 将设置为另一个 UIImage。因此,我有两个变量用于已检查和未检查图像。

UIImage *checkedImage = [UIImage imageNamed:@"check_box_checked_white.png"];
UIImage *uncheckedImage= [UIImage imageNamed:@"check_box_unchecked_white.png"];

点击按钮会执行下面的方法

- (void)checkButton:(UIButton *)sender {

NSLog(@"current %@", UIImagePNGRepresentation(sender.imageView.image));
NSLog(@"checked %@", UIImagePNGRepresentation(checkedImage));
NSLog(@"unchecked %@", UIImagePNGRepresentation(uncheckedImage));

if ([UIImagePNGRepresentation(sender.imageView.image) isEqualToData:UIImagePNGRepresentation(checkedImage)]){

[sender setImage: [UIImage imageNamed:@"check_box_unchecked_white.png"] forState: UIControlStateNormal];
}

else if ([UIImagePNGRepresentation(sender.imageView.image) isEqualToData:UIImagePNGRepresentation(uncheckedImage)]){

[sender setImage: [UIImage imageNamed:@"check_box_checked_white.png"] forState: UIControlStateNormal];
} }

两个 if 子句的语句都不为真,对于 NSLog 语句,控制台显示了这一点

当前 <89504e47 0d0a1a0a 0000000d 49484452 00000019 00000013 08060000 006572a6 05000000 01735247 4200aece 1ce90000 001c6944 4f5 40000 00020000 00000000 000a0000 00280000 000a0000 00090000 01066167 14d70000 00d24944 4154480d b48d3d0a c2401046 7304c142 11 6f6021 587a121b 1b1b151b 1b5b6f21 5a780183 781425e0 45442b09 ab6f7037 99950125 9081d9f9 7bdf7e49 526338e7 66e4be36 0b3e9fbe 08 aaabc5 847fe762 2041bf88 4c589c3e a76a2ffa 1d390c6a fa756420 03cb6300 aa54f459 d0c95f85 01c3400e d43325f5 fdb800fe 68d0ae44 a7228d64 004dd209 40c97d9d 44d08f01 dd3218d0 5f4d9c43 9f7c2a70 6482c612 5d57e92e 0652ae00 db086e22 a0e664ab bcda1d4c 877c2893 8d4daa2d 82431078 b39e3a47 2df706fc ​​fd8bdf46 901fde 00 0000ffff f01a55c5 000000cf 49444154 ad92bd09 02411484 4f453136 b014c1cc 5030b403 41033b10 0b10ccc5 16446cc0 d02a844b 4d8d4e53 57b f39df c2b226e7 eac0e3cd fb9b81db cbb200ce b9c31390 0b65013e 0a564a4a bb4d3fb7 f905be30 be8d773f 6a96bdc9 187ed4a1 009ffb65 ca16f5d9 fa05bc43 24994c24 caf14e62 027c6dbd 93d5377a 5deba59b 98 c0aa74 791bddcd e081414f 7301fe9b 8944109e 054668ba befa1e7f 31911842 43e28ad9 c08bfb9c 6a3245ac 4134a3a8 45b5e675 4c9664bd 5bf5bf4b 0729a864 82f09e45 410ffb6d e86ee33f 61985f0d b75066ce 264e2e00 00000049 454e44ae 426082>

已检查<89504e47 0d0a1a0a 0000000d 49484452 00000019 00000013 08060000 006572a6 05000000 01735247 4200aece 1ce90000 001c6944 4f 540000 00020000 00000000 000a0000 00280000 000a0000 00090000 01130cba f03c0000 00df4944 4154480d b4913b0a c2401086 7304c142 116 f6021 587a121b 1b1b151b 1b5b6f21 5a780183 781425e0 45442b71 b37ea3fb 84051f90 81ecccce 7cfffc24 c9b20a43 6b3d514a 6d2bb3c0 60cca34b 2121376 4fc5c0c4 2c32c1f4 6027ff64 3ecd065d 3fd02e23 03b960b2 0f809f4b f48515c9 2e6740b3 27039a47 522e3531 74c01705 fce2a5f2 471ec9e8 d7312865 4e7a186e 14 411f2e 68e64627 3bce491c a0cbf06e 41f22009 269ab06d ab63c729 81f81660 13e82202 b2bc51c3 4fd3154c 0bf6261a 097efc2a 4d065da0 dd1b776 7 27184725 440d83ab 2329d0af 23c85c9e 000000ff ffc36350 d7000000 d3494441 54a5923d 0ac24014 84a3a258 5b7814c1 ce52b0f4 06422cbc 817 800c1 5ebc8288 17b0f414 82adad55 b4ddd56f c20b2c6b 6336032f f3fe7666 59926501 bcf7e70f 800bb161 16ac9429 fd3e3b77 cde107b4 56ee9c3b c4bb3f75 65c2fe9c fca28386 55b54cdd 6376531f d6650644 92c942a2 dce c2831 817ca71e c257d5f0 0b1aaa07 a79b4800 f1ad4405 84dfc60e 1e692e90 37333191 a5c4058c 3c342ed5 ed43dddc c48ca6e8 3f119c84 06364b32 c9 11eb10 dd285a51 ad799bd8 10b5ff2e 9da98dbf 7e61544f 7a73e012 c263b28f 9f52f517 246950a5 7cf4e6af 00000000 49454e44 ae426082>

未选中<89504e47 0d0a1a0a 0000000d 49484452 00000018 00000012 08060000 0041ec1e 9e000000 01735247 4200aece 1ce90000 001c6944 4f 540000 00020000 00000000 00090000 00280000 00090000 00090000 0065c6d0 72350000 00314944 41543811 62601805 a484c07f 0a0141bb 2 834ff3f d1161054 88a600e6 3034614c 2ed10ad1 b412ad8f 688583c9 02000000 00ffff66 fa95da00 00003949 44415463 604002ff a1004988 2826d1fa 885 68866 2dd1fa88 56386a01 5a0810e4 121db444 2b44b392 687d442b a4d40298 45a4d268 f682b900 f6d17e90 3cc7b79b 00000000 49454e44 ae426082>

但是我还没有对中间执行的代码中的图像做任何事情。可能是什么原因导致(在这种情况下)当前图像和选中的图像不一样?

谢谢!

最佳答案

使用 UIButton 的 currentImage 属性和 isEqual 方法进行图像比较,如下所示:

UIButton *btnClicked = (UIButton *)sender;
UIImage *checkedImage = [UIImage imageNamed:@"check_box_checked_white.png"];
UIImage *uncheckedImage= [UIImage imageNamed:@"check_box_unchecked_white.png"];

if([btnClicked.currentImage isEqual:checkedImage]) //Check if it has checked image or not
{
//so set unchecked image
[btnClicked setImage:uncheckedImage forState: UIControlStateNormal];
}
else //button has unchecked image
{
//so set checked image
[btnClicked setImage:checkedImage forState: UIControlStateNormal];
}

编辑:正如@Michael Olenick 在评论中所建议的,最好是使用UIButton 的 selected 属性。 UIButton 上的默认 state 将为 NOunchecked image

if(btnClicked.selected) //Check if it has checked image or not
{
//so set unchecked image
[btnClicked setImage:uncheckedImage forState: UIControlStateNormal];
}
else //button has unchecked image
{
//so set checked image
[btnClicked setImage:checkedImage forState: UIControlStateNormal];
}

btnClicked.selected = !btnClicked.selected;

关于ios - 为什么与 UIImagePNGRepresentation 的比较不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29229112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com