简体   繁体   中英

Setting UIImageView image from ViewController

iPhone n00b here, although I do have an app in the store.

I have two simple ViewControllers, taken directly from the Utility Application template in xCode. I have two UIImageViews , one on each ViewController in a storyboard. I have the outlets hooked up correctly (as far as I know) because I can set the image of the first ViewController fine with

[self.imageView setImage:@"test.png"]

When I try to do essentially the same thing in my FlipsideViewController , nothing happens.

- (void)selectImage:(UIImage *)img
{
    NSLog(@"%@", img);
    self.editImageView.image = img;
    NSLog(@"%@", self.editImageView.image);
}

This code, gives the correct result after logging the first NSLog statement, but the second line does not have the desired effect, and the third line yields (null) .

EDIT: the coed is updated to reflect the fact that I want to display "img" rather than another image initialized using imageNamed , that was simply a test.

If you're passing a UIImage to your method, why are you then setting the imageView via the imageNamed: function?

If you're passing the correct UIImage then you should just do the following.

- (void)selectImage:(UIImage *)img
{
    NSLog(@"%@", img); // This is the UIImage being passed.
    [self.editImageView setImage:img];
    NSLog(@"%@", self.editImageView.image);
}

From what I can see that is all that is needed. If this is wrong, please update your question so I can answer accordingly.

Edited due to comments

So, from the comments I have gathered what the issue is.

You're saying that in -(void)viewDidLoad you can set the image, that is easy, with the [UIImage imageNamed:] method. That's fine, but you want to do it in a separate method which is causing the issue.

What I'd suggest is doing the following, for testing sakes.

 - (void)viewDidLoad
 {
      [super viewDidLoad];
      // Do any additional setup after loading the view from its nib.

      UIImage *imageYouWantToPass = [UIImage imageNamed:@"test.png"];
      [self selectImage:imageYouWantToPass];

 }

Make sure that the method selectImage: is added to your .h file so that you don't get any warnings. I think this is what the answer is, but if this still doesn't resolve your question please provide more information.

[self.imageView setImage:[UIImage imagenamed:@"test.png"]]

There u are missing..

And in second case. It seems that you have connected imageView to .xib but not the property.Try using it without self. Or declare IBOutlet in property

Check whether editImageView is initialized properly or not. First NSLog works proper means you have proper image and second NSLog does not work mean there might be a problem with editImageView .

As you're passing a UIImage to your method( selectImage: ) Then No need to set the imageView (UIImageView) via the imageNamed: method. Because you have UIImage in the img instance, you can just set the image from img within the function.

If you're passing the correct UIImage then you should just do the following.

- (void)selectImage:(UIImage *)img
{   
     NSLog(@"%@", img); // here you have Image in img, so just set Image From this img.

     [self.editImageView setImage:img];
     NSLog(@"%@", self.editImageView.image);
}

I hope it clears everything...!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM