How to compare two UIImage objects?

Discussion in 'iOS Programming' started by Sergio10, May 10, 2009.

  1. Sergio10 macrumors regular

    Joined:
    Oct 3, 2007
    #1
    Hi,

    I have two objects: image1 and image2 with UIImage type.

    Need to know are they init with "myImage.png" or not.
    How to compare?
    Could you please provide the code?

    Thanks.
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    You could try hashing, though its an inexact science. I would suggest using NSObject's isEqual: but I guess they won't be the same object, just two objects referencing the same file.
     
  3. Sergio10 thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #3
    IsEqual doesn't compare(
    I wrote:
    PHP:
    UIImage *image1 = [UIImage imageNamed: @"myImage.png"];
    UIImage *image2 = [UIImage imageNamed: @"myImage.png"];

    if([
    someImage isEqualTopatternImage])   // never true (((
    {
               
    // some code
    }
    So, How to know that my UIImage objects initialized with the same image?
    Please provide me a code.
    Thanks.
     
  4. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    What research have you already done on this?

    You can't just expect free code and that will help round down the location of a solution.
     
  5. Sergio10 thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #5
    So,

    I suggest to get pixel data(using CGImage) and compare it. How ti do it?
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    imageNamed: returns a cached object, so for basic purposes you could just try checking the pointers:
    Code:
    if (image1 == image2)
    If that doesn't work you could cache the image objects yourself and then compare.

    Other than that, you'll have to do some pixel comparisons which won't be pretty.
     
  7. Sergio10 thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #7
    So,

    I suggest to get pixel data(using CGImage) and compare it. How ti do it?
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    Forget about comparing pixels.

    UImage doesn't keep a name of the file it was inited with. You need to do that part yourself. You could subclass UIImage and add a name property to it. Compare the names to find if they're the same.

    Else, you can keep your own dictionary that uses the UIImage* as the key and the name as value. Or keep a couple mutable arrays with UIImages and names as the values.
     

Share This Page