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

Posted

技术标签:

【中文标题】从照片库中选择时,应用程序崩溃 iPhone Sim【英文标题】:App crashes iPhone Sim when selecting from Photo Library 【发布时间】:2011-10-31 16:46:42 【问题描述】:

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

我这辈子都不明白为什么。我见过很多关于 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];

【问题讨论】:

您的模拟器上的照片库可能已损坏。尝试在 iPhone 模拟器上重置内容和设置。 我认为你可能是对的,sgosha。我刚刚通过 Safari 应用添加了我自己的照片,它很喜欢那张。感谢您的建议! 【参考方案1】:

试试这个,它对我有用。

- (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 我都用过;它使用某种时间戳保存图片。

希望对您有所帮助。

【讨论】:

时间戳非常方便,但您的回调方法现在实际上已被弃用。我的应用程序似乎甚至没有进入回调,因为它在组装其参数时会引发错误。不过感谢您的及时回复:)

以上是关于从照片库中选择时,应用程序崩溃 iPhone Sim的主要内容,如果未能解决你的问题,请参考以下文章

应用程序在 iPhone5 上的照片中保存图像时崩溃,但在其他设备中运行良好

当我从 iCloud 中选择照片时,应用程序崩溃了

图像选择器崩溃 - iOS Swift

iphone,如果可能的话,通过 UIImagePickerController 从照片库中选择多个文件

从 Google 照片库加载图像以进行裁剪

iPad 模拟器不适用于 iPhone 应用程序中的 UIImagePickerController