Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 702
swamy Avatar asked Dec 11 '22 10:12

swamy


2 Answers

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");
}
like image 184
iPatel Avatar answered Dec 13 '22 22:12

iPatel


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"]) {
   ...
}
like image 40
HalR Avatar answered Dec 13 '22 22:12

HalR