UIImage 从 UIImagePickerController 获取 nil
UIImage is getting nil from the UIImagePickerController
我正在尝试更新 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,它的所有引用出口和变量都被设置为新的。在您的情况下,userPicImage
是作为新对象启动的,因此从逻辑上讲,其中不会有任何内容。
- 您最好将图像数据存储在
userdefaults
中并从那里检索。
尝试此代码,因为当您再次加载时 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];
}
}];
}
我正在尝试更新 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,它的所有引用出口和变量都被设置为新的。在您的情况下,userPicImage
是作为新对象启动的,因此从逻辑上讲,其中不会有任何内容。 - 您最好将图像数据存储在
userdefaults
中并从那里检索。
尝试此代码,因为当您再次加载时 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];
}
}];
}