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
Setting UIImageView image from ViewController
提问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 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.
编辑:更新 coed 以反映我想显示“img”而不是使用 初始化的另一个图像的事实imageNamed
,这只是一个测试。
回答by Sebastien Peek
If you're passing a UIImage
to your method, why are you then setting the imageView
via the imageNamed:
function?
如果您将 a 传递UIImage
给您的方法,那么为什么要imageView
通过该imageNamed:
函数设置 the ?
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. 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 .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.
确保该方法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 imageView
to .xib
but not the property.Try using it without self. Or declare IBOutlet
in property
在第二种情况下。似乎您已连接imageView
到.xib
但未连接到该属性。尝试在没有 self 的情况下使用它。或IBOutlet
在财产中申报
回答by Apurv
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
.
检查是否editImageView
正确初始化。第一个 NSLog 正常工作意味着您有正确的图像,第二个 NSLog 不起作用意味着editImageView
.
回答by Kamar Shad
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.
当您将 a 传递UIImage
给您的方法(selectImage:
)时,则无需imageView (UIImageView)
通过该imageNamed:
方法设置。因为你UIImage
在img
实例中,你可以在函数中设置image
from img
。
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...!!!!
我希望它清除一切......!