【问题标题】:How do I get the data from UIImagePickerControllerReferenceURL?如何从 UIImagePickerControllerReferenceURL 获取数据?
【发布时间】:2012-05-29 12:54:30
【问题描述】:

我在我的应用程序中使用ELCImagePickerController,我不想将选定的 fullScreenImage 保存到我的数组中,因为如果我选择了 40 张 iPad 图像,那就不好了。

我想从方法- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info 的字典中从UIImagePickerControllerReferenceURL 而不是UIImagePickerControllerOriginalImage 获取数据。

我试过了:

NSDictionary *dict = [info objectAtIndex:count];            

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dict objectForKey:@"UIImagePickerControllerReferenceURL"]]]];//UIImagePNGRepresentation([UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@",[dict objectForKey:@"UIImagePickerControllerReferenceURL"]]] );
        NSLog(@"length %d",[data length]);
        imageview.image = [UIImage imageWithData:data];

但是,每次我得到 0 个字节。我已经尝试了论坛中所有可用的答案,但没有用。

谁能回答这个问题?

【问题讨论】:

    标签: objective-c uiimagepickercontroller


    【解决方案1】:

    UIImagePickerControllerReferenceURL 返回NSURL 对象而不是字符串对象。请将您的代码更改为 -

    NSData *data = [NSData dataWithContentsOfURL:[dict objectForKey:@"UIImagePickerControllerReferenceURL"]];
    NSLog(@"length %d",[data length]);
    imageview.image = [UIImage imageWithData:data];
    

    UIImagePickerControllerReferenceURLAssets Library返回NSURL对象,所以你可以得到图像-

    ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
    [assetLibrary assetForURL:[[self.imagedata objectAtIndex:i] valueForKey:UIImagePickerControllerReferenceURL] 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];//this is NSData may be what you want
        [data writeToFile:photoFile atomically:YES];//you can save image later
    } failureBlock:^(NSError *err) {
        NSLog(@"Error: %@",[err localizedDescription]);
    }];
    

    注意:ALAssetsLibrary 在 iOS 9 中已弃用。

    【讨论】:

    • 感谢您的回复...我也尝试过这样,即使在我的日志中我的长度为 0 "2012-05-29 18:32:13.536 myapp[1793:707] length 0 "
    • 它是 0,因为 UIImagePickerControllerReferenceURL 返回资产库的 NSURL 对象。所以你需要使用 Assets Library 从这个 url 获取图像。作为 - NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL]; ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; [库assetForURL:referenceURL resultBlock:^(ALAsset *asset) { ... } failureBlock:^(NSError *error) { ... }];
    • 对于它的价值 ALAssetsLibrary 在 iOS 9 中已被弃用。我在下面添加了一个额外的答案。
    • @Jessedc,感谢您的通知,我在回答中添加了弃用说明。
    【解决方案2】:

    这个问题在 Google 上的 UIImagePickerControllerReferenceURL 排名很高,所以我想我会在 iOS9 及更高版本中添加正确的使用 UIImagePickerControllerReferenceURL 的方法,因为 ALAssetLibrary 已被弃用,以支持照片框架。

    使用来自imagePickerController(_:didFinishPickingMediaWithInfo:) 的信息字典中提供的UIImagePickerControllerReferenceURL 访问照片的正确方法是通过照片工具包PHAsset

    UIImagePickerControllerDelegate 利用 Photos Framework 获取 UIImage 的基本实现如下所示:

    class YourViewController: UIViewController, UIImagePickerControllerDelegate
    {
        public func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]?) {
            guard let info = info, let url = info[UIImagePickerControllerReferenceURL] as? NSURL else {
                // Using sourceType .Camea will end up in here as there is no UIImagePickerControllerReferenceURL
                picker.dismissViewControllerAnimated(true) {}
                return
            }
    
            let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([url], options: nil)
            if let photo = fetchResult.firstObject as? PHAsset {
                PHImageManager.defaultManager().requestImageForAsset(photo, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFill, options: nil) {
                    image, info in
                    // At this point you have a UIImage instance as image
                }
            }
        }
    }
    

    当 sourceType 为 .Camera 时,上面的代码不会处理回调,因为信息字典不包含 UIImagePickerControllerReferenceURL

    【讨论】:

    • 注意 UIImagePickerControllerReferenceURL 在 iOS 11.0 中也被弃用了,所以可以使用条件 NSString *infoKey; if (@available(iOS 11, *)) { infoKey = UIImagePickerControllerPHAsset; } else { infoKey = UIImagePickerControllerReferenceURL; }
    • PHAsset.fetchAssetsWithALAssetURLs 现在也被弃用了。
    【解决方案3】:

    在 ELCImagePickers "Selected assets" 中你可以做

    -(void)selectedAssets:(NSArray*)_assets {
    
    "... your code .?.?."
    
        NSMutableArray *returnArray = [[NSMutableArray alloc] init];
    
        for(ALAsset *asset in _assets) {
    
            NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
            [workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];
    
    ".. any other properties you need ?"
    
            [returnArray addObject:workingDictionary];
    
        }
    }
    

    然后在你的其他类中从数组中保存

    - (void) importImagesFromArray:(NSArray *)_images toFolder:(NSString *)folderPath
    {
       if ([_images count] > 0) {
    
        //... YOUR CODE HERE FOR CHECKING YOUR ARRAY...Maybe a loop or something//
        //
        //
        //
    
        //ex:
    
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        for (NSDictionary *dict in _images) {
    
    
    
            [library assetForURL:[dict objectForKey:@"UIImagePickerControllerReferenceURL"]
                     resultBlock:^(ALAsset *asset){
    
                         //You Can Use This
    
                         UIImage *theImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]
                                                                 scale:1.0
                                                           orientation:[[asset valueForProperty:@"ALAssetPropertyOrientation"] intValue]];
    
                         //[....save image blah blah blah...];
    
                         ///////////////////////////////////////////////////
                         ///////////////////////////////////////////////////
    
                         ////// OR YOU CAN USE THIS////////////////////
    
                         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];//this is NSData may be what you want
                         [data writeToFile:[folderPath stringByAppendingPathComponent:@"Some Filename You Need To Assign"] atomically:YES];
    
    
                     }
    
                    failureBlock:^(NSError *error){
                        NSLog(@"Error saving image");
    
                    }];
    
            // Dont forget to release library
    
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 2012-06-14
      • 2020-01-29
      • 2021-02-10
      相关资源
      最近更新 更多