简体   繁体   English

从ViewController设置UIImageView图像

[英]Setting UIImageView image from ViewController

iPhone n00b here, although I do have an app in the store. iPhone n00b在这里,尽管我在商店中有一个应用程序。

I have two simple ViewControllers, taken directly from the Utility Application template in xCode. 我有两个简单的ViewController,直接取自xCode中的Utility Application模板。 I have two UIImageViews , one on each ViewController in a storyboard. 我有两个UIImageViews ,在故事板上的每个ViewController上一个。 I have the outlets hooked up correctly (as far as I know) because I can set the image of the first ViewController fine with 我已经正确连接了插座(据我所知),因为我可以使用以下方法很好地设置第一个 ViewController的图像

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

When I try to do essentially the same thing in my FlipsideViewController , nothing happens. 当我尝试在FlipsideViewController执行基本相同的操作时,什么也没发生。

- (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) . 此代码在记录第一个NSLog语句后给出正确的结果,但是第二行没有达到预期的效果,第三行产生(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. 编辑:男女同校已更新,以反映我要显示“ img”而不是使用imageNamed初始化的另一张图片的事实,那只是一个测试。

If you're passing a UIImage to your method, why are you then setting the imageView via the imageNamed: function? 如果你传递一个UIImage ,以你的方法,你为什么那么设置imageView通过imageNamed:功能?

If you're passing the correct UIImage then you should just do the following. 如果要传递正确的UIImage则应该执行以下操作。

- (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. 您说的是,在-(void)viewDidLoad ,可以使用[UIImage imageNamed:]方法来设置图像,这很容易。 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. 确保将selectImage:方法添加到您的.h文件中,以免收到任何警告。 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. 看来您已连接imageView.xib但使用它没有自我不是property.Try。 Or declare IBOutlet in property 或在属性中声明IBOutlet

Check whether editImageView is initialized properly or not. 检查editImageView是否正确初始化。 First NSLog works proper means you have proper image and second NSLog does not work mean there might be a problem with editImageView . 第一个NSLog工作正常意味着您拥有正确的映像,第二个NSLog工作不正常意味着editImageView可能存在问题。

As you're passing a UIImage to your method( selectImage: ) Then No need to set the imageView (UIImageView) via the imageNamed: method. 当您将UIImage传递给方法( selectImage:无需通过imageNamed:方法设置selectImage: imageView (UIImageView) Because you have UIImage in the img instance, you can just set the image from img within the function. 因为您在img实例中有UIImage ,所以您只需在函数中设置来自imgimage

If you're passing the correct UIImage then you should just do the following. 如果要传递正确的UIImage则应该执行以下操作。

- (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...!!!! 我希望它清除所有内容... !!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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