swamy
swamy

Reputation: 53

UIImageView image name comparision Issue

Recently I found an issue regarding UIImageView image.

My Intention is to find If UIImageView Contains the 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
}

In normal run the above code is working fine.

But it fails to work when application came to active state once it was sent to background.

Upvotes: 4

Views: 4193

Answers (6)

Hardik Mamtora
Hardik Mamtora

Reputation: 1652

Yes you can compare with the help of data like below

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

Upvotes: 1

Muruganandham K
Muruganandham K

Reputation: 5331

try this

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
}

Upvotes: 1

Nathan Day
Nathan Day

Reputation: 6047

I would use the image to store state information also you don't know if isEqual: is comparing what you are interesting in. If its just using the isEqual it inherits from NSObject then it will compare just UIImage addresses, this could explain why you are having issues, perhaps UIImageView is using CGImage or some lower level object internally and is creating a UIImage as need.

Upvotes: 1

iPatel
iPatel

Reputation: 47099

Generally UIImageView can not store file name of image but it store only UIImage so, it is not possible to get file name of image which you add in UIImageView.

Thats way if you want to compare only image file name then it is not possible to compare it.

But If you want to compare two UIImage object then

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

Upvotes: 9

HalR
HalR

Reputation: 11083

The ios will intelligently re-use images and not make new ones if it can reuse the same image. That is why your comparison works some times. It is a bad technique and should not be relied upon.

You would be better served by keeping a reference to the string used to generate the first image, and comparing it to see if it contained image.png.

@property NSString *imageName;

then when you create your UIImage, with [UIImage imageNamed:yourNameString] you can save the string used with imageName = yourNameString;

Then later on, just check

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

Upvotes: 1

Rugmangathan
Rugmangathan

Reputation: 3226

You should compare image data instead of comparing like the above code you mentioned..

there are two things to keep in mind when comparing UIImage, they are

  1. if you are using UIImage with imageName for both image you can use isEqual
  2. whereas, in different case you must compare image data instead using the above way..

Upvotes: 0

Related Questions