如何从UIImagePickerControllerReferenceURL获取数据?

时间:2012-05-29 12:54:30

标签: objective-c uiimagepickercontroller

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

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

我试过了:

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字节。我尝试过在论坛中提供的所有答案,但没有用。

有人可以回答这个吗?

3 个答案:

答案 0 :(得分:26)

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

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

UIImagePickerControllerReferenceURL会为NSURL返回Assets Library个对象,因此您可以将图像设为 -

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]);
}];

注意:在iOS 9中不推荐使用ALAssetsLibrary。

答案 1 :(得分:10)

此问题在Google上排名UIImagePickerControllerReferenceURL,因此我认为我会在 iOS9及更高版本中添加使用UIImagePickerControllerReferenceURL的正确方法,因为ALAssetLibrary已被弃用赞成照片框架。

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

使用Photos Framework获取UIImage的UIImagePickerControllerDelegate的基本实现看起来像这样:

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

答案 2 :(得分:1)

在ELCImagePickers“选定的资产”中你可以做到

-(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

    }
}
}