即使我得到确认,图像也没有保存
它的工作原理是这样的,所以如果有人遇到类似的问题,你可以这样做
所以这是我在 applicationdidfinishlaunching 中放入的内容
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"image"] == nil) {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
这是我在
(void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSData *imageData;
UIImage *yourUIImage;
imageData = [NSKeyedArchiver archivedDataWithRootObject:yourUIImage];
[[NSUserDefaults standardUserDefaults] setObject:imageData forKey:@"image"];
yourUIImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissModalViewControllerAnimated:YES];}
It works like this, so if someone having a similar problem, you can just do it like this
So here is what I put in the applicationdidfinishlaunching
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"image"] == nil) {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
Heres what I wrote in the
(void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSData *imageData;
UIImage *yourUIImage;
imageData = [NSKeyedArchiver archivedDataWithRootObject:yourUIImage];
[[NSUserDefaults standardUserDefaults] setObject:imageData forKey:@"image"];
yourUIImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissModalViewControllerAnimated:YES];}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在序列化(归档)未初始化的变量。变量
yourUIImage
已声明,但您从未为其分配任何内容。然后你尝试将其存档。如果你幸运的话,yourUIImage
只是nil
,但如果你不幸的话,它会因为未初始化的垃圾指针而崩溃。You are serializing (archiving) an uninitialized variable. The variable
yourUIImage
is declared, but you never assign anything to it. Then you try to archive it. If you're lucky,yourUIImage
simply isnil
, but if you're unlucky it would crash due to the unitialized garbage pointer.这可能听起来很愚蠢,但是您在设置对象后调用了
[[NSUserDefaults standardUserDefaults] Synchronize]
吗?It might sound stupid, but have you called
[[NSUserDefaults standardUserDefaults] synchronize]
after setting your object?