10

我在我的应用程序中使用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 个字节。我已经尝试了论坛中所有可用的答案,但没有用。

请问有人能回答这个吗?

4

3 回答 3

26

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

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

UIImagePickerControllerReferenceURL返回NSURL对象 for 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]);
}];

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

于 2012-05-29T12:59:01.810 回答
10

这个问题在谷歌上排名很好,UIImagePickerControllerReferenceURL所以我想我会添加UIImagePickerControllerReferenceURLiOS9 和更高版本中使用的正确方法,因为ALAssetLibrary已被弃用,取而代之的是照片框架。

使用imagePickerController(_:didFinishPickingMediaWithInfo:)UIImagePickerControllerReferenceURL的信息字典中提供的访问照片的正确方法是通过 Photos Kit 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 为 since 时,上面的代码不会处理回调,.Camera因为 info 字典不包含UIImagePickerControllerReferenceURL.

于 2016-07-20T08:16:59.147 回答
1

在 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

    }
}
}
于 2013-01-07T18:27:47.040 回答