Error comparing UIImageView image names

I recently found a problem with the UIImageView image.

My intention is to find If UIImageView Contains an image (image.png) or not.

if([[self.ImgView.image isEqual:[UIImage imageNamed:@"image.png"]])
{
   NSLog(@"Image View contains image.png");//In normal run....
}else
{
  NSLog(@"Image View doesn't contains image.png");//Once came back from background
}

With a normal start, the above code works fine.

But it does not work when the application goes into an active state after it is sent to the background.

+4
source share
6 answers

Usually UIImageViewit cannot save the image file name, but it only saves UIImage, therefore, it is not possible to get the name of the image file that you add to UIImageView.

This is the way to go, if you want to compare only the image file name, then it cannot be compared.

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");
}
+10

ios , . . , .

, , , , , image.png.

@property NSString *imageName;

UIImage [UIImage imageNamed: yourNameString] , imageName = yourNameString;

,

 if ([imageName isEqualToString:@"image.png"]) {
   ...
}
+1

, , isEqual: , . isEqual, NSObject, UIImage, , , UIImageView CGImage - UIImage .

+1

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
}
+1

, ,

    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;
    }
+1

, .

, UIImage,

  • If you use UIImage with the image name for both images, you can use isEqual
  • whereas in another case, you must compare image data using the above method.
0
source

All Articles