Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Nnavick

macrumors regular
Original poster
Oct 13, 2010
100
0
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 !
 
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.
 
U could make an UIImage pointer which holds a specific image name. and then compare to this pointer.
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?
 
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?

I think he meant that because UIImage holds the image attributes we can compere them but unfortunately it's not possible.
 
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
 
there is no chance to wrote youruiimageviewname.image.

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?!
 
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).
 
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).

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) {
   }
?
 
and how do I compare a UIButton image to UIImage ?

Code:
UIButton *btt;;
        UIImage *buttonImage = [UIImage imageNamed:@"abc.png"];
    if(btt.imageView.image==buttonImage) {
   }
?
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.
 
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.

can you write an example?
thanks
 
can you write an example?

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.
 
Last edited:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.