ios - UIImage not retrieving From NSUserDefaults -
in application grab image picker , try display in separate view.
in mainviewcontroller.m have
-(void)imagepickercontroller:(uiimagepickercontroller*)picker didfinishpickingmediawithinfo:(nsdictionary*)info { [picker dismissviewcontrolleranimated:yes completion:^ { self.image = [info objectforkey:@"uiimagepickercontrolleroriginalimage"]; nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; [defaults setobject:uiimagepngrepresentation(self.image) forkey:@"imagekey"]; [defaults synchronize]; publishviewcontroller *publish = [self.storyboard instantiateviewcontrollerwithidentifier:@"publish"]; [self presentviewcontroller:publish animated:yes completion:nil]; }]; }
i try retrieve image in publishviewcontroller.m
- (void)viewdidload { [super viewdidload]; // additional setup after loading view. nsdata *imagedata = [[nsuserdefaults standarduserdefaults] objectforkey:@"imagekey"]; uiimage *userimage = [uiimage imagewithdata:imagedata]; self.image = [[uiimageview alloc] initwithimage:userimage]; }
the image not show though. reason why?
perhaps -viewdidload
in publishviewcontroller.m called before completion block in dismissviewcontrolleranimated:completion:
, nsdata has not been saved in nsuserdefaults, yet.
put code load self.image
in publishviewcontroller.m -viewdidappear
.
being said agree commenters nsuserdefaults may not best place store image data.
Comments
Post a Comment