didFinishPickingMediaWithInfo返回零照片

我正在努力捕获在4.0中使用返回的图像

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [[picker parentViewController] dismissModalViewControllerAnimated:YES]; // MediaType can be kUTTypeImage or kUTTypeMovie. If it's a movie then you // can get the URL to the actual file itself. This example only looks for images. // NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType]; // NSString* videoUrl = [info objectForKey:UIImagePickerControllerMediaURL]; // Try getting the edited image first. If it doesn't exist then you get the // original image. // if (CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) { UIImage* picture = [info objectForKey:UIImagePickerControllerEditedImage]; if (!picture) picture = [info objectForKey:UIImagePickerControllerOriginalImage]; // **picture is always nil // info dictionary count = 1 } } 

发生的事情是信息字典总是返回一个单一的条目:

{UIImagePickerControllerMediaType =“public.image”;

这是伟大的,但从来没有一个形象。

我正在使用这个论坛的一个很好的例子来做到这一点,我很确定这些电话是正确的,但从来没有一个形象。

提前致谢!

我知道这是几个月后,但我挣扎了几个小时,并发现这个网站和iphonedevsdk.com全部相同的问题,但从来没有一个工作的答案。

总而言之,如果图像是从照相机卷/照片库中挑选出来的,但是如果图像是照相机拍摄的新照片,它将无法正常工作。 那么,这里是如何使它适用于两个:

尝试对info字典进行任何操作之前,必须closures并释放UIImagePickerController。 要超清晰:

这不工作:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // No good with the edited image (if you allowed editing) myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage]; // AND no good with the original image myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage]; // AND no doing some other kind of assignment UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage]; [picker dismissModalViewControllerAnimated:YES]; [picker release]; } 

在所有这些情况下,图像将是零。

但是,通过释放UIImagePickerController的魔术

这是否工作:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissModalViewControllerAnimated:YES]; [picker release]; // Edited image works great (if you allowed editing) myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage]; // AND the original image works great myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage]; // AND do whatever you want with it, (NSDictionary *)info is fine now UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage]; } 

疯狂的简单,但这就是它的一切。

而马修·弗雷德里克的答案是最受欢迎的,早在dismissViewControllerAnimated:completion: ,苹果就提供了dismissViewControllerAnimated:completion:来取代现在已经废弃的(iOS 6.0以上版本) dismissViewControllerAnimated:

在完成块中执行图像信息字典检索应该希望对所有人更有意义。

从上面看他的例子,现在看起来像:

 - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:^{ // Edited image works great (if you allowed editing) myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage]; // AND the original image works great myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage]; // AND do whatever you want with it, (NSDictionary *)info is fine now UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage]; }]; } 

我已经尝试了以上所有,但没有运气的iPad 6.0 / 6.1模拟器,但是我发现信息包含“UIImagePickerControllerReferenceURL”键,这里是我的代码:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:NULL]; UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage]; if(NULL == image){ [MyImageLoader loadImageFromAssertByUrl:[info objectForKey:@"UIImagePickerControllerReferenceURL"] completion:^(UIImage* img){ //img not null here }]; }else{ //image not null here } } 

而loadImageFromAssertByUrl的代码是:

 +(void) loadImageFromAssertByUrl:(NSURL *)url completion:(void (^)(UIImage*)) completion{ ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init]; [assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) { ALAssetRepresentation *rep = [asset defaultRepresentation]; Byte *buffer = (Byte*)malloc(rep.size); NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil]; NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES]; UIImage* img = [UIImage imageWithData:data]; completion(img); } failureBlock:^(NSError *err) { NSLog(@"Error: %@",[err localizedDescription]); }]; } 

还有一些版本的XCode和Mountain Lion的已知错误。 检查你的控制台这个消息:

2013-01-17 21:36:34.946 3sure-ios[5341:1803] Named service 'com.apple.PersistentURLTranslator.Gatekeeper' not found. assetsd is down or misconfigured. Things will not work the way you expect them to.

如果出现这个消息,这可能不会起作用。 在实际的iOS设备上检查您的应用程序。

Nevetheless,重新启动模拟器和XCode似乎解决了这个问题。

使用

 [[UIImagePickerController alloc] init]; 

代替

 [[UIImagePickerController alloc] initWithNibName:(NSString *) bundle:(NSBundle *)]; 

我有相同的症状,但在我的情况下,事实certificate,我有UIImagePickerController层次结构中的某个类别覆盖“uuid”或“setUUID”? 我猜CoreData需要这些方法,或者非常非常困惑,资产库是所有CoreData的东西。

 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:NULL]; //Image picker for nil value UIImage* selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage]; NSLog(@"my Info :: %@", info); if(NULL == selectedImage){ UIImage * selectedImage = [PhotoViewController loadImageFromAssertByUrl:[info objectForKey:@"UIImagePickerControllerReferenceURL"] completion:^(UIImage * selectedImage){ //img not null here NSLog(@"img2 ::: %@", selectedImage); uploadImg.image = selectedImage; [self.view addSubview:PermissionView]; }]; }else{ //image not null here NSLog(@"Up IMG00 :: %@", uploadImg.image); NSLog(@"Sel IMG00 :: %@", selectedImage); uploadImg.image = [selectedImage retain]; NSLog(@"Up IMG22 :: %@", uploadImg.image); NSLog(@"Sel IMG22 :: %@", selectedImage); } } +(UIImage *) loadImageFromAssertByUrl:(NSURL *)url completion:(void (^)(UIImage*)) completion{ __block UIImage* img; ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init]; [assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) { ALAssetRepresentation *rep = [asset defaultRepresentation]; Byte *buffer = (Byte*)malloc(rep.size); NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil]; NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES]; img = [UIImage imageWithData:data]; completion(img); NSLog(@"img ::: %@", img); } failureBlock:^(NSError *err) { NSLog(@"Error: %@",[err localizedDescription]); }]; return img; } 

以下步骤需要遵循:
1)制作UIActionSheet
2)在select必要的源types – 加载resrouces
3)didFinishPickingMediaWithInfo – 设置图像从图像字典收到

  - (IBAction)actionButtonUpload:(id)sender { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera",@"Photo Library", nil]; [actionSheet showInView:self.view]; } #pragma mark -- UIActionSheet Delegate -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; UIAlertView *alert; switch (buttonIndex) { case 0: if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { NSLog(@"No camera!"); picker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:picker animated:YES completion:NULL]; } else { alert = [[UIAlertView alloc]initWithTitle:@"Alumnikonnect" message:@"Camera is not available, please select a different media" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } break; case 1: if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:picker animated:YES completion:NULL]; } else { alert = [[UIAlertView alloc]initWithTitle:@"Alumnikonnect" message:@"Photolibrary is not available, please select a different media" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } break; } } #pragma mark - Image picker delegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { self.imageEventPic.layer.cornerRadius = self.imageEventPic.frame.size.height /2; self.imageEventPic.layer.masksToBounds = YES; self.imageEventPic.layer.borderWidth = 0; self.imageEventPic.layer.borderWidth = 1.0f; self.imageEventPic.layer.borderColor = [UIColor colorWithRed:0.212 green:0.255 blue:0.302 alpha:1.000].CGColor; self.labelUploadPic.hidden = YES; self.imageEventPic.image = info[UIImagePickerControllerOriginalImage]; [self dismissViewControllerAnimated:YES completion:NULL]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self dismissViewControllerAnimated:YES completion:NULL]; } -(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize; { UIGraphicsBeginImageContext( newSize ); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }