UIImagePickerController使我的应用程序崩溃!

时间:2010-04-16 15:01:01

标签: iphone uiimagepickercontroller nsuserdefaults nskeyedarchiver

在我的应用中,用户可以选择图像,将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 - 我不知道该命名它,所以我开玩笑并将它命名为...... :)提前致谢!

1 个答案:

答案 0 :(得分:1)

您无法直接归档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!

-Stephen