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”;


很棒,但是从来没有图像。


我在这个论坛上使用了一个很好的例子来做到这一点,我很确定调用是正确的,但是从来没有图像。


梵蒂冈之花
浏览 1606回答 3
3回答

慕的地10843

尽管Matthew Frederick的回答最受欢迎,并且长期以来一直是适当的回应,从iOS 5.0(苹果可用)开始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]);  }];}
打开App,查看更多内容
随时随地看视频慕课网APP