【发布时间】: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];
}
}];
}
【问题讨论】:
-
嗯,你检查
didFinishPickingMediaWithInfo和loadUserProfile中的self是不是同一个地址? -
查看这篇文章并根据您的要求修改代码。 theappguruz.com/blog/ios-image-picker-controller
标签: ios objective-c uiimage uiimagepickercontroller