xcode 从 ViewController 设置 UIImageView 图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9527905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:29:51  来源:igfitidea点击:

Setting UIImageView image from ViewController

iphoneobjective-ciosxcodeuiimageview

提问by steve sullivan

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. 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 firstViewController fine with

我有两个简单的 ViewController,直接取自 xCode 中的 Utility Application 模板。我有两个UIImageViews,一个在情节提要中的每个 ViewController 上。我有网点迷上了正确的(据我所知),因为我可以设置的图像第一的ViewController罚款

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

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

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

当我尝试在 my 中做本质上相同的事情时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 NSLogstatement, 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.

编辑:更新 coed 以反映我想显示“img”而不是使用 初始化的另一个图像的事实imageNamed,这只是一个测试。

回答by Sebastien Peek

If you're passing a UIImageto your method, why are you then setting the imageViewvia the imageNamed:function?

如果您将 a 传递UIImage给您的方法,那么为什么要imageView通过该imageNamed:函数设置 the ?

If you're passing the correct UIImagethen 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)viewDidLoadyou 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.

您是说在-(void)viewDidLoad您可以使用该[UIImage imageNamed:]方法设置图像时,这很容易。这很好,但您想以导致问题的单独方法进行操作。

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 .hfile 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.

确保该方法selectImage:已添加到您的.h文件中,以免收到任何警告。我认为这就是答案,但如果这仍然不能解决您的问题,请提供更多信息。

回答by DivineDesert

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

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

There u are missing..

你失踪了..

And in second case. It seems that you have connected imageViewto .xibbut not the property.Try using it without self. Or declare IBOutletin property

在第二种情况下。似乎您已连接imageView.xib但未连接到该属性。尝试在没有 self 的情况下使用它。或IBOutlet在财产中申报

回答by Apurv

Check whether editImageViewis 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.

检查是否editImageView正确初始化。第一个 NSLog 正常工作意味着您有正确的图像,第二个 NSLog 不起作用意味着editImageView.

回答by Kamar Shad

As you're passing a UIImageto your method(selectImage:) Then No need to set the imageView (UIImageView)via the imageNamed:method. Because you have UIImagein the imginstance, you can just set the imagefrom imgwithin the function.

当您将 a 传递UIImage给您的方法(selectImage:)时,则无需imageView (UIImageView)通过该imageNamed:方法设置。因为你UIImageimg实例中,你可以在函数中设置imagefrom img

If you're passing the correct UIImagethen 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...!!!!

我希望它清除一切......!