简体   繁体   English

UIImageView未设置图像

[英]UIImageView not setting image

Im trying to set a UIImageView from a segue, and for some reason the image is not getting set.. Heres the .h files of my class that subclassed a UIViewController 我试图从一个设置中设置一个UIImageView ,并且由于某种原因图像没有被设置。这是我的类的.h文件,该类UIViewControllerUIViewController

@interface PhotoDisplayViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *photoView;
-(void)setPhoto:(UIImage *)photo;
@end

and heres the setPhoto 这是setPhoto

-(void)setPhoto:(UIImage *)photo{
    NSLog(@"PHOTO %@", photo);
    _photoView.image = photo;
    NSLog(@"MYPHOTO %@", _photoView.image);
}

when i call setPhoto from prepare for segue, i see this in the console 当我从准备拍照调用setPhoto时,我在控制台中看到了

2012-12-16 13:26:22.129 TestApp[2183:907] PHOTO <UIImage: 0x1fd7cd80>
2012-12-16 13:26:22.130 TestApp[2183:907] MYPHOTO (null)

Why is this happening? 为什么会这样呢?

It looks like _photoView is probably nil . 看起来_photoView可能为nil It may not be set when loading the nib. 装入笔尖时可能未设置。 Make sure you've wired it up properly in IB. 确保已正确连接到IB。 Or perhaps you are calling -setPhoto: before the view has loaded. 或者,可能是在加载视图之前调用-setPhoto:。

I would advise you to change the weak to strong and take a look on this link. 我劝你在改变weakstrong ,并借此链路上的样子。

Objective-C - weak property - getter autoreleases (Automatic Reference Counting) Objective-C-弱属性-吸气剂自动释放(自动引用计数)

I think you called setPhoto: method as following. 我认为您调用了setPhoto:方法,如下所示。 In this situation, first setPhoto: is called and after that viewDidLoad method is called in PhotoDisplayViewController. 在这种情况下,首先调用setPhoto ::,然后在PhotoDisplayViewController中调用viewDidLoad方法。 That's why photoView is nil. 这就是为什么photoView为零的原因。 Actually viewDidLoad method should be called in PhotoDisplayViewController.So you should change the code slightly. 实际上,应该在PhotoDisplayViewController中调用viewDidLoad方法。因此,应稍微更改代码。

-(void)someActionMethod{
          [self performSegueWithIdentifier:yourIdentifier sender:nil];
 }
 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
            if ([[segue identifier] isEqualToString:yourIdentifier]) {
                photoDisplayVC = [segue destinationViewController];//photoDisplayVC is a object of         PhotoDisplayViewController
                 [photoDisplayVC setPhoto:imageObject];
 }

The change Code: In this situation first viewDidLoad method is called and after that setPhoto: method is called in PhotoDisplayViewController. 更改代码:在这种情况下,首先调用viewDidLoad方法,然后在PhotoDisplayViewController中调用setPhoto:方法。 Then you will get image. 然后,您将获得图像。

-(void)someActionMethod{
  [self performSegueWithIdentifier:yourIdentifier sender:nil];
 [photoDisplayVC setPhoto:imageObject];
   }
  -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
                if ([[segue identifier] isEqualToString:yourIdentifier]) {
                    photoDisplayVC = [segue destinationViewController];//photoDisplayVC is a object of         PhotoDisplayViewController

     }

I think it will be helpful to you. 我认为这将对您有所帮助。

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

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