0

I have a UIImageView that holds a cover photo for a user. Through the xcode IDE I added a placeholder image that acts as a stock cover photo. When the user clicks on the cover photo I need a way to tell if the image inside the UIImageView is the stock cover photo or another photo that the user has added. The image is never nil. Currently I am doing it by comparing the image data of the current cover photo to the image data generated buy creating an image from the stock cover photo image file.

This is what I have:

if ([UIImagePNGRepresentation(_coverPhoto.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:@"stockCoverPhoto.png"])])
{
    [self loadPhoto:1];
}

Comparing the _coverPhoto.image to UIImage imageNamed:@"stockCoverPhoto.png" produced buggy results when the app was closed and reopened within the same session. There must be a better way to do this, I need my code to be as efficient as possible and comparing two NSData structures to one another seems unnecessary.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
ScottOBot
  • 839
  • 3
  • 16
  • 37
  • 3
    Why don't you make the image in the `UIImageView` *nil* in **IB**, and then when your `viewWillAppear:` method is called, check if the `.image` property of the `UIImageView` is *nil*, and if it is, add the placeholder image, and if it is not *nil*, you know it was a user added image. – msgambel May 31 '13 at 20:20
  • interesting way of going about it, I will try and see if that works (don't see why it wouldn't) and get back to you @msgambel – ScottOBot May 31 '13 at 20:30

2 Answers2

2

try with this

    UIImage *img = [UIImage imageNamed:@"some.png"];
    UIImage *img1 = [UIImage imageNamed:@"some1.png"];

    NSData *dataObj1 = UIImagePNGRepresentation(img);
    NSData *dataObj2 = UIImagePNGRepresentation(img2);

    BOOL test = [dataObj1 isEqualToData:dataObj2];

     if(test) 
         //is Equal

   and other answer is

NSData compare to NSData in percents
NSData isEqualtoData

Community
  • 1
  • 1
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45
0

I suppose user will not add this picture every time he start the app. I don't see the problem at all. Just use some property for this, handle it after loading user picture and save it to NSUSerDefaults, cloud or somewhere...

// your class interface
@property (nonatomic) BOOL isUserPictureLoaded;
// first time run save isUserPictureLoaded = NO to NSUserDefaults
// if user saves own picture, set isUserPictureLoaded = YES and save to NSUserDefaults
alexhajdu
  • 410
  • 5
  • 13