我正在尝试更新Viewcontroller上的图像,正在使用ImagePickerController,我能够看到chosenImage包含数据,并进行了分配。当选择图像,然后再次加载此viewcontroller时,我可以进行调试以查看它是否符合loadUserProfile方法,是的。但是UIImage在某个地方以某种方式无效。

@property (strong, nonatomic) UIImage *userPicImage;
@property (weak, nonatomic) IBOutlet UIImageView *userProfileImage;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadUserProfile];
}

-(void)loadUserProfile
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString * userImageURL = [defaults objectForKey:@"imageURL"];
    bool isReload = [defaults boolForKey:@"isReload"];

    if(isReload)
    {
         //self.userPicImage is always nil
         [self.userProfileImage setImage:self.userPicImage];
         [defaults setBool:false forKey:@"comingBack"];
         [defaults synchronize];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImagePickerControllerSourceType source = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
   // userPicImage is not nil here!
    self.userPicImage = chosenImage;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:true forKey:@"isReload"];
    [defaults synchronize];

    [picker dismissViewControllerAnimated:YES completion:^{
        if (source == UIImagePickerControllerSourceTypeCamera) {
            [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
        }
    }];
}

最佳答案

  • 每当调用viewdidload时,就会将viewcontroller作为新的reference加载,其所有参考出口和变量都设置为new。在您的情况下,userPicImage是作为新对象启动的,因此在逻辑上不会包含任何内容。
  • 最好将图像数据存储在userdefaults中,然后从那里检索它。
  • 关于ios - UIImage从UIImagePickerController获取零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42479087/

    10-16 21:10