[英]App crashes iPhone Sim when selecting from Photo Library
我正在嘗試使用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];
}
試試這個,對我有用。
- (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; 它以某種時間戳保存圖片。
希望這對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.