Ios UIImageView图像名称比较问题
最近我发现了一个关于UIImageView图像的问题 我的目的是查找UIImageView是否包含图像(image.png) 在正常运行中,上述代码工作正常Ios UIImageView图像名称比较问题,ios,iphone,objective-c,uiimageview,Ios,Iphone,Objective C,Uiimageview,最近我发现了一个关于UIImageView图像的问题 我的目的是查找UIImageView是否包含图像(image.png) 在正常运行中,上述代码工作正常 但当应用程序发送到后台后进入活动状态时,它无法工作。通常UIImageView不能存储图像的文件名,但它只存储UIImageso, 无法获取您在UIImageView中添加的图像的文件名 这样,如果只想比较图像文件名,则无法进行比较 但是如果你想比较两个UIImage对象,那么 UIImage *secondImage = [UIImage
但当应用程序发送到后台后进入活动状态时,它无法工作。通常
UIImageView
不能存储图像的文件名,但它只存储UIImage
so,
无法获取您在UIImageView
中添加的图像的文件名
这样,如果只想比较图像文件名,则无法进行比较
但是如果你想比较两个UIImage
对象,那么
UIImage *secondImage = [UIImage imageNamed:@"image.png"];
NSData *imgData1 = UIImagePNGRepresentation(self.imageView.image);
NSData *imgData2 = UIImagePNGRepresentation(secondImage);
BOOL isCompare = [imgData1 isEqual:imgData2];
if(isCompare)
{
NSLog(@"Image View contains image.png");
}
else
{
NSLog(@"Image View doesn't contains image.png");
}
您应该比较图像数据,而不是像上面提到的代码那样进行比较 在比较UIImage时,有两件事需要记住,它们是
ios将智能地重复使用图像,如果可以重复使用相同的图像,则不会生成新图像。这就是为什么你的比较有时有效。这是一种糟糕的技术,不应该依赖 最好是保留对用于生成第一个图像的字符串的引用,并对其进行比较以查看是否包含image.png
@property NSString *imageName;
然后,在创建UIImage时,使用[UIImage imageName:yourNameString]可以保存与imageName=yourNameString一起使用的字符串
然后,稍后再检查
if ([imageName isEqualToString:@"image.png"]) {
...
}
我会用图像来存储状态信息,你也不知道isEqual:是否在比较你感兴趣的内容。如果只是使用从NSObject继承的isEqual,那么它将只比较UIImage地址,这可以解释出现问题的原因,也许UIImageView在内部使用CGImage或某个较低级别的对象,并根据需要创建UIImage。试试这个
float numDifferences = 0.0f;
float totalCompares = width * height / 100.0f;
for (int yCoord = 0; yCoord < height; yCoord += 10) {
for (int xCoord = 0; xCoord < width; xCoord += 10) {
int img1RGB[] = [image1 getRGBForX:xCoord andY: yCoord];
int img2RGB[] = [image2 getRGBForX:xCoord andY: yCoord];
if (abs(img1RGB[0] - img2RGB[0]) > 25 || abs(img1RGB[1] - img2RGB[1]) > 25 || abs(img1RGB[2] - img2RGB[2]) > 25) {
//one or more pixel components differs by 10% or more
numDifferences++;
}
}
}
if (numDifferences / totalCompares <= 0.1f) {
//images are at least 90% identical 90% of the time
}
else {
//images are less than 90% identical 90% of the time
}
float numDifferences=0.0f;
浮点数=宽度*高度/100.0f;
对于(int-yCoord=0;yCoord25 | abs(img1RGB[1]-img2RGB[1])>25 | abs(img1RGB[2]-img2RGB[2])>25){
//一个或多个像素组件相差10%或更多
numDifferences++;
}
}
}
如果(numDifferences/totalCompares是,您可以借助下面的数据进行比较
UITableViewCell *cell = (UITableViewCell*)[self.view viewWithTag:indexPath.row + 100];
UIImage *secondImage = [UIImage imageNamed:@"boxhover.png"];
NSData *imgData1 = UIImagePNGRepresentation(cell.imageView.image);
NSData *imgData2 = UIImagePNGRepresentation(secondImage);
BOOL isCompare = [imgData1 isEqual:imgData2];
if(isCompare)
{
//contain same image
cell.imageView.image = [UIImage imageNamed:@"box.png"];
}
else
{
//does not contain same image
cell.imageView.image = secondImage;
}
您初始化了一个要比较的UIImage
?看起来占用内存。您正在比较图像名称,为什么不尝试IsequalString:这不是内存问题。它也不适用于小图像。您从哪里获得宽度、高度和getRGBForX
的?
UITableViewCell *cell = (UITableViewCell*)[self.view viewWithTag:indexPath.row + 100];
UIImage *secondImage = [UIImage imageNamed:@"boxhover.png"];
NSData *imgData1 = UIImagePNGRepresentation(cell.imageView.image);
NSData *imgData2 = UIImagePNGRepresentation(secondImage);
BOOL isCompare = [imgData1 isEqual:imgData2];
if(isCompare)
{
//contain same image
cell.imageView.image = [UIImage imageNamed:@"box.png"];
}
else
{
//does not contain same image
cell.imageView.image = secondImage;
}