0

在我的應用程序中,用戶可以選擇圖像,將UIImageView的圖片更改爲新圖像,並使用NSKeyedArchiver將圖像保存到NSUserDefaults。當用戶選擇圖像並調用- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info時,該應用立即崩潰! Xcode收到錯誤:SIGABRT。這裏是我的代碼 -UIImagePickerController導致我的應用程序崩潰!

 

- (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的名字!

+2

如果你把細節,比如在哪裏以及如何你appliaction崩潰這將提高你得到答案的機會。例如哪條線,調試日誌等 – 2010-04-16 15:02:28

+0

對不起,我現在修好了! – Flafla2 2010-04-16 22:18:21

回答

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

相關問題