从照片库中选择时,应用程序会崩溃iPhone Sim

时间:2011-10-31 16:46:42

标签: iphone ios

我正在尝试使用UIImagePickerController从照片库中选择照片(UIImagePickerControllerSourceTypePhotoLibrary)。 我的问题是,当我选择一张照片时,应用程序会立即崩溃。从下面给出的错误中,它尝试在nil中插入NSDictionary并返回照片。

无法理解为什么对我的生活。我见过很多关于UIImagePickerController的崩溃报告,但都没有解决这个问题。那里有什么想法吗?

下面是控制台输出,下面是我的代码。

2011-10-31 12:29:27.195 LognLoad[49923:7703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: UIImagePickerControllerOriginalImage)'
*** Call stack at first throw:
(
0   CoreFoundation                      0x02392c99 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x024e05de objc_exception_throw + 47
2   CoreFoundation                      0x0234b3f8 +[NSException raise:format:arguments:] + 136
3   CoreFoundation                      0x0234b36a +[NSException raise:format:] + 58
4   CoreFoundation                      0x02391505 -[__NSCFDictionary setObject:forKey:] + 293
5   PhotoLibrary                        0x0b47414e __PLNotifyImagePickerOfImageAvailability_block_invoke_1 + 161
6   PhotoLibrary                        0x0b515ac1 __-[PLAssetsSaver requestImageFromAsset:withFormat:completionBlock:synchronous:]_block_invoke_1 + 45
7   MediaPlayer                         0x0b9e7141 PUTReceivedImageFromAssetURL + 169
8   MediaPlayer                         0x0b9e88c7 do_ReturnImageDataForAssetURL + 225
9   MediaPlayer                         0x0b9e8c4e _XReturnImageDataForAssetURL + 435
10  MediaPlayer                         0x0b9e8a64 PersistentURLTranslatorClient_server + 125
11  libSystem.B.dylib                   0x972d06fb dispatch_mig_server + 232
12  MediaPlayer                         0x0b9e871d __getClientMIGMux_block_invoke_1 + 45
13  libSystem.B.dylib                   0x972aa498 _dispatch_source_latch_and_call + 62
14  libSystem.B.dylib                   0x9729d3d2 _dispatch_source_invoke + 210
15  libSystem.B.dylib                   0x9729bf59 _dispatch_queue_invoke + 163
16  libSystem.B.dylib                   0x9729c495 _dispatch_queue_drain + 258
17  libSystem.B.dylib                   0x9729bee8 _dispatch_queue_invoke + 50
18  libSystem.B.dylib                   0x9729bcfe _dispatch_worker_thread2 + 240
19  libSystem.B.dylib                   0x9729b781 _pthread_wqthread + 390
20  libSystem.B.dylib                   0x9729b5c6 start_wqthread + 30
)terminate called after throwing an instance of 'NSException'

在这里,我参与了图片选择器:

- (IBAction)getImage
{
// Create image picker controller

self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:self.imgPicker animated:YES];

}

这是图像选择的回调:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.photo = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

1 个答案:

答案 0 :(得分:0)

试试这个,它对我有用。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
    NSString *hour = [components hour] < 10 ? [NSString stringWithFormat:@"0%i", [components hour]] : [NSString stringWithFormat:@"%i", [components hour]];
    NSString *minute = [components minute] < 10 ? [NSString stringWithFormat:@"0%i", [components minute]] : [NSString stringWithFormat:@"%i", [components minute]];
    NSString *second = [components second] < 10 ? [NSString stringWithFormat:@"0%i", [components second]] : [NSString stringWithFormat:@"%i", [components second]];
    NSString *day = [components day] < 10 ? [NSString stringWithFormat:@"0%i", [components day]] : [NSString stringWithFormat:@"%i", [components day]];
    NSString *month = [components month] < 10 ? [NSString stringWithFormat:@"0%i", [components month]] : [NSString stringWithFormat:@"%i", [components month]];
    NSString *year = [NSString stringWithFormat:@"%i", [components year]];
    NSString *fileName = [NSString stringWithFormat:@"%@_%@_%@_%@_%@_%@.png", day, month, year, hour, minute, second];
    NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@", fileName]];
    [UIImagePNGRepresentation(img) writeToFile:pngPath atomically:YES];

    if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera)
        UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

我用过PhotoAlbum和Camera;它用一种时间戳保存图片。

希望这对你有所帮助。