UIImageView is equal to

Discussion in 'iOS Programming' started by Nnavick, Apr 28, 2011.

  1. macrumors regular

    Joined:
    Oct 13, 2010
    #1
    Hi,
    how can I compare the image name inside the UIImageView to string?
    Like the image name is "abc.png" and I want to say if the image name is equal to "abc.png" do something.

    Thank !
     
  2. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    The image view and the image itself have no concept of a name. To implement that you need to do it yourself in your code.

    You could do this with a simple ivar holding the name that matches the image view. It could be done with a dictionary that has the object pointer as the key and the name as the value. Undoubtedly other ways also.
     
  3. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    U could make an UIImage pointer which holds a specific image name. and then compare to this pointer.
     
  4. thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #4
    I tried it....it's not working
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Please elaborate, jnoxx. Cuz I'm not sure what solution you are proposing here. How can a UIImage pointer hold an image name when a UIImage pointer simply references a UIImage object which has no name property?
     
  6. thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #6
    I think he meant that because UIImage holds the image attributes we can compere them but unfortunately it's not possible.
     
  7. macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #7
    I believe he meant.
    Code:
    UIImage *Image1 = [UIImage imageNamed:@"abc.png"];
    
    	if(youruiimageviewname.image == Image1){
    	//do stuff
    }
    
     
  8. thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #8
    there is no chance to wrote youruiimageviewname.image.
     
  9. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #9
    No, I meant it like this.
    Code:
    UIImage *image = [UIImage imageNamed:@"whatever.jpg"];
    so u know image pointer holds the imagenamed whatever.jpg, so u can check if this exist.
    Code:
    if (image)
    { // Do whatever}
    else {
    //dayum, create a new UIImage pointer then!
    }
    Not sure if this is what u want, just inserting idea's ;o
     
  10. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #10
    And what he meant was

    Code:
    UIImage *image = [UIImage imageNamed:@"whatever.jpg"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        
        if (imageView.image == image) {
            //wow, it's the same, i'm gonna do some kickass stuff here
        } else {
            //Damn, it isn't the same.. let's do something else..
        }
    What bout that?!
     
  11. macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #11
    Originally Posted by Nnavick
    "there is no chance to wrote youruiimageviewname.image."

    ?

    youruiimageviewname is where you would place your UIImageView name (your IBOutlet name for your UIImageView).
     
  12. thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #12
    I thought it's UIImage...my mistake.

    and how do I compare a UIButton image to UIImage ?

    Code:
    UIButton *btt;;
            UIImage *buttonImage = [UIImage imageNamed:@"abc.png"];
        if(btt.imageView.image==buttonImage) {
       }
    
    ?
     
  13. macrumors 603

    Joined:
    Aug 9, 2009
    #13
    Step 1: Refer to the UIButton class reference.
    Does it have an imageView property?
    If not, then what properties or methods does it have that return the UIImage* type?
    Maybe the imageForState or backgroundImageForState properties.

    Step 2: Once you've determined the property or method to use on UIButton, determine how you wish to compare the UIImage pointers.
    The obvious == operation only compares whether two object pointers are identical. It's possible to have images loaded from the same file whose object pointers are different. If your code is written so all loading of images uses UIImage imageNamed:, then that condition (two pointers with same image file but different value) probably won't happen.

    Step 3: Once you have the proper method or property, and have decided on what you'll compare the images with, write the conditional statement.

    Step 4: Debug your code.
    If it works first time, great. If not, then determine whether any variables (such as the UIButton or UIImage) happen to be nil. Use the debugger for this.
     
  14. thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #14
    can you write an example?
    thanks
     
  15. chown33, Apr 30, 2011
    Last edited: Apr 30, 2011

    macrumors 603

    Joined:
    Aug 9, 2009
    #15
    An example of what?

    Your original question was about UIImageView and its image. Is that what you want an example of?

    Your latest posted code was about a UIButton and an imageView property you seem to think it has. Unfortunately, UIButton has no imageView property, so your code won't work, and I can't possibly write an example using an imageView property. Well, I could, but it wouldn't work either.

    A UIButton can have multiple images. You should know this if you performed Step 1 that I gave before: Read the UIButton class reference. Since you haven't said which image of the UIButton you want to compare to, I have no way of knowing what to write as an example.

    So Step 1 is still for your to read the UIButton class reference and figure out which of the UIButton images you want to use.
     
  16. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #16
    Nick, you should describe what the overall goal is. As mentioned, UIImage, UIImageView, and UIButton have no concept of the name of an image. Tell us what you're really trying to do and you'll get better suggestions on how to do it.
     

Share This Page