【问题标题】:UIImage is getting nil from the UIImagePickerControllerUIImage 从 UIImagePickerController 得到零
【发布时间】:2017-02-27 06:24:11
【问题描述】:

我正在尝试更新Viewcontroller 上的图像,我正在使用ImagePickerController,我可以看到chosenImage 有数据,我分配了它。 When image is chosen and then this viewcontroller is loading again, I could able to debug to see whether or not it hits loadUserProfile method, yes it is.但是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];
        }
    }];
}

【问题讨论】:

标签: ios objective-c uiimage uiimagepickercontroller


【解决方案1】:
  • 每当viewdidload 被调用时,viewcontroller 被加载为一个新的reference,它的所有引用出口和变量都设置为新的。在您的情况下,userPicImage 是作为新对象启动的,因此在逻辑上不会有任何内容。
  • 您最好将图像数据存储在userdefaults 并从那里检索。

【讨论】:

  • 非常有帮助?
【解决方案2】:

试试这个代码,因为当你再次加载 vc 时,图像数据为零,所以首先将图像数据转换为 base64string。

@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
        NSString *base64String=[defaults stringForKey:@"chosenimage"];
        NSData* data = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
        UIImage* image = [UIImage imageWithData:data];
        self.userProfileImage.image=image;
      //  [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;
    NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1.0);
    NSString *encodedString = [imageData base64Encoding];

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

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

【讨论】:

    猜你喜欢
    • 2016-01-10
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多