PDA

View Full Version : UIImageView is equal to




Nnavick
Apr 28, 2011, 03:45 PM
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 !



PhoneyDeveloper
Apr 28, 2011, 04:48 PM
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.

jnoxx
Apr 29, 2011, 02:49 AM
U could make an UIImage pointer which holds a specific image name. and then compare to this pointer.

Nnavick
Apr 29, 2011, 05:39 AM
U could make an UIImage pointer which holds a specific image name. and then compare to this pointer.

I tried it....it's not working

dejo
Apr 29, 2011, 07:01 AM
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?

Nnavick
Apr 29, 2011, 09:56 AM
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.

nickculbertson
Apr 29, 2011, 12:08 PM
I believe he meant.

UIImage *Image1 = [UIImage imageNamed:@"abc.png"];

if(youruiimageviewname.image == Image1){
//do stuff
}

Nnavick
Apr 29, 2011, 12:49 PM
I believe he meant.

UIImage *Image1 = [UIImage imageNamed:@"abc.png"];

if(youruiimageviewname.image == Image1){
//do stuff
}


there is no chance to wrote youruiimageviewname.image.

jnoxx
Apr 29, 2011, 01:11 PM
No, I meant it like this.
UIImage *image = [UIImage imageNamed:@"whatever.jpg"];

so u know image pointer holds the imagenamed whatever.jpg, so u can check if this exist.
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

jnoxx
Apr 29, 2011, 01:14 PM
there is no chance to wrote youruiimageviewname.image.

And what he meant was

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

nickculbertson
Apr 29, 2011, 01:14 PM
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).

Nnavick
Apr 29, 2011, 04:50 PM
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 ?

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

?

chown33
Apr 29, 2011, 05:53 PM
and how do I compare a UIButton image to UIImage ?

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.

Nnavick
Apr 30, 2011, 03:23 AM
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

chown33
Apr 30, 2011, 12:07 PM
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.

PhoneyDeveloper
Apr 30, 2011, 01:54 PM
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.