UIImagePickerController 导致我的应用程序崩溃!

发布于 2024-08-29 11:58:04 字数 2817 浏览 6 评论 0原文

在我的应用程序中,用户可以选择图像,将 UIImageView 的图片更改为新图像,然后使用 NSKeyedArchiver 将图像保存到 NSUserDefaults。当用户选择图像并调用 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 时,应用程序立即崩溃! Xcode 收到错误:SIGABRT。这是我的代码 -


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"PictureKey"] == 1) {
        UIImage *previmage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [prevbutton setImage:previmage];
        NSData *previmagedata = [NSKeyedArchiver archivedDataWithRootObject:previmage];
        [[NSUserDefaults standardUserDefaults] setObject:previmagedata forKey:@"PrevImage"];
    } else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"PictureKey"] == 2) {
        UIImage *nextimage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [nextbutton setImage:nextimage];
        NSData *nextimagedata = [NSKeyedArchiver archivedDataWithRootObject:nextimage];
        [[NSUserDefaults standardUserDefaults] setObject:nextimagedata forKey:@"NextImage"];
    }else {
        UIImage *optionsimage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [moreoptionsbutton setImage:optionsimage];
        NSData *optimagedata = [NSKeyedArchiver archivedDataWithRootObject:optionsimage];
        [[NSUserDefaults standardUserDefaults] setObject:optimagedata forKey:@"OptionsImage"];
    }
}

这是我的错误。


wait_fences: failed to receive reply: 10004003
2010-04-16 18:09:25.012 iPimpYourMusicApp[2815:6807] Failed to save the videos metadata to the filesystem. Maybe the information did not conform to a plist.
2010-04-16 18:09:49.009 iPimpYourMusicApp[2815:207] *** -[UIImage encodeWithCoder:]: unrecognized selector sent to instance 0x1a1180
2010-04-16 18:09:49.016 iPimpYourMusicApp[2815:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIImage encodeWithCoder:]: unrecognized selector sent to instance 0x1a1180'
2010-04-16 18:09:49.026 iPimpYourMusicApp[2815:207] Stack: (
    864992541,
    859229716,
    864996349,
    864492313,
    864454720,
    854367759,
    854367021,
    854365681,
    27619,
    841224192,
    857776904,
    857868032,
    857637052,
    839607712,
    839985680,
    854089611,
    864740547,
    864738335,
    875880904,
    838872112,
    838865456,
    11173,
    11068
)
terminate called after throwing an instance of 'NSException'
[Switching to thread 11779]
Program received signal:  “SIGABRT”.

不要嘲笑我的应用程序的名称是 iPimpYourMusicApp - 我不知道该给它命名什么,所以我开玩笑地命名它......:) 提前致谢!

In my app, a user can select an image, change the picture of a UIImageView to the new imge, and save the image to NSUserDefaults using NSKeyedArchiver. When the user selects the image and the - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info is called, the app immediately crashes! Xcode recieves the error: SIGABRT. Here is my code -


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"PictureKey"] == 1) {
        UIImage *previmage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [prevbutton setImage:previmage];
        NSData *previmagedata = [NSKeyedArchiver archivedDataWithRootObject:previmage];
        [[NSUserDefaults standardUserDefaults] setObject:previmagedata forKey:@"PrevImage"];
    } else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"PictureKey"] == 2) {
        UIImage *nextimage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [nextbutton setImage:nextimage];
        NSData *nextimagedata = [NSKeyedArchiver archivedDataWithRootObject:nextimage];
        [[NSUserDefaults standardUserDefaults] setObject:nextimagedata forKey:@"NextImage"];
    }else {
        UIImage *optionsimage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [moreoptionsbutton setImage:optionsimage];
        NSData *optimagedata = [NSKeyedArchiver archivedDataWithRootObject:optionsimage];
        [[NSUserDefaults standardUserDefaults] setObject:optimagedata forKey:@"OptionsImage"];
    }
}

And here is my error.


wait_fences: failed to receive reply: 10004003
2010-04-16 18:09:25.012 iPimpYourMusicApp[2815:6807] Failed to save the videos metadata to the filesystem. Maybe the information did not conform to a plist.
2010-04-16 18:09:49.009 iPimpYourMusicApp[2815:207] *** -[UIImage encodeWithCoder:]: unrecognized selector sent to instance 0x1a1180
2010-04-16 18:09:49.016 iPimpYourMusicApp[2815:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIImage encodeWithCoder:]: unrecognized selector sent to instance 0x1a1180'
2010-04-16 18:09:49.026 iPimpYourMusicApp[2815:207] Stack: (
    864992541,
    859229716,
    864996349,
    864492313,
    864454720,
    854367759,
    854367021,
    854365681,
    27619,
    841224192,
    857776904,
    857868032,
    857637052,
    839607712,
    839985680,
    854089611,
    864740547,
    864738335,
    875880904,
    838872112,
    838865456,
    11173,
    11068
)
terminate called after throwing an instance of 'NSException'
[Switching to thread 11779]
Program received signal:  “SIGABRT”.

Don't laugh at the name of my app being iPimpYourMusicApp - I had no idea what to name it, so I joked around and named it that... :) Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

初相遇 2024-09-05 11:58:04

您不能直接存档 UIImage - UIImage 不实现 NSCoding。您必须使用 UIImage[JPEG|PNG]Representation 之一将其转换为 NSData 对象。

所以你的代码看起来像:

UIImage *optionsimage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[moreoptionsbutton setImage:optionsimage];
[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(optionsimage) forKey:@"OptionsImage"];

取回图像需要相反的操作:

UIImage *optImage = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"OptionsImage"]];

HTH!

-史蒂芬

You can't directly archive a UIImage - UIImage doesn't implement NSCoding. You have to use one of the UIImage[JPEG|PNG]Representation to convert it to a NSData object.

So you code would look like:

UIImage *optionsimage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[moreoptionsbutton setImage:optionsimage];
[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(optionsimage) forKey:@"OptionsImage"];

Getting the image back out would require the inverse:

UIImage *optImage = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"OptionsImage"]];

HTH!

-Stephen

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