即使我得到确认,图像也没有保存

发布于 2024-12-27 05:53:22 字数 873 浏览 5 评论 0原文

它的工作原理是这样的,所以如果有人遇到类似的问题,你可以这样做

所以这是我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃温暖了心ぐ 2025-01-03 05:53:22

您正在序列化(归档)未初始化的变量。变量 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 is nil, but if you're unlucky it would crash due to the unitialized garbage pointer.

浪菊怪哟 2025-01-03 05:53:22

这可能听起来很愚蠢,但是您在设置对象后调用了 [[NSUserDefaults standardUserDefaults] Synchronize] 吗?

It might sound stupid, but have you called [[NSUserDefaults standardUserDefaults] synchronize] after setting your object?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文