Nick
Nick

Reputation: 368

How to change the image inside an imageview? xCode 4.2

I've been looking for hours now and i cant find a solution. i have an imageview, i have declare it as an IBOutlet, inside that imageview i have already put an image using the interface build of xCode 4.2.

Now my quesion, whats the line of code that it can change the image of the imageview?

The old ones like UIImage *image = [UIImageView imageNamed:@"image2.png"];

its not working because it says that *image is unused variable. it also gives me error at imageNamed it says No known class method for selector 'imageName'

Whats the new code in 4.2? or is it me doing something wrong?

Upvotes: 0

Views: 14251

Answers (5)

Lucio Fonseca
Lucio Fonseca

Reputation: 3527

This Works fine for me. oldImage is the IBOutlet of UIImageView. The setCellImage method is inside the Custom UITableViewCell Class.

- (void)setCellImage:(UIImage*)newImage{



[self.oldImage setImage:newImage];


}

Upvotes: 0

zaph
zaph

Reputation: 112873

You need to assign

[UIImage imageNamed:@"image2.png"]

to the image property of the ivar of the IBOutlet.

For example if your IBOutlet is defined:

@property (nonatomic, strong) IBOutlet UIImageView *imageView;

assign like so:

imageView.image = [UIImage imageNamed:@"image2.png"];

Upvotes: 0

Caleb
Caleb

Reputation: 125037

myImageView.image = [UIImage imageNamed:@"image2.png"];

That should do it. UIImageView has an image property that you can set, either as above, or using the -setImage: method that the property provides. Also, from the docs:

Setting the image property does not change the size of a UIImageView. Call sizeToFit to adjust the size of the view to match the image.

Your other questions:

its not working because it says that *image is unused variable.

In the code you provided, you're assigning the image to a local variable. If you don't then do something with that variable, there's no point in that code. Instead, assign the image to the image property of your image view, as described above.

it also gives me error at imageNamed it says No known class method for selector 'imageName'

That just looks like a typo in your code. The lack of a 'd' in 'imageName' is surely the problem.

Whats the new code in 4.2?

Nothing has changed with respect to setting the image of an image view, as far as I know. I think you've just got a couple little issues in your code.

Upvotes: 4

iDifferent
iDifferent

Reputation: 2210

You are trying to declare a new variable and not using the ivar you declared with the IBOutlet

Upvotes: 0

Eimantas
Eimantas

Reputation: 49354

It's you doing something wrong. You are mixing up UIImage and UIImageView classes. The former is for representing image data in memory. The latter one is representing UIImage instances in a view.

You should change your line to:

imageViewOutlet.image = [UIImage imageNamed:@"image2.png"];

Notice that I'm using UIImage class to assign the image to UIImageView's image property.

Upvotes: 1

Related Questions