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.
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");
}
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"]) {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With