Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何从UIImagePickerControllerReferenceURL获取数据?_Objective C_Uiimagepickercontroller - Fatal编程技术网

Objective c 如何从UIImagePickerControllerReferenceURL获取数据?

Objective c 如何从UIImagePickerControllerReferenceURL获取数据?,objective-c,uiimagepickercontroller,Objective C,Uiimagepickercontroller,我正在我的应用程序中使用,我不想将所选的全屏图像保存到我的阵列中,因为如果我选择了40张iPad图像,那就不好了 我想从UIImagePickerControllerReferenceURL获取数据,而不是从UIImagePickerControllerOriginalImage方法的目录中获取数据-(void)elcImagePickerController:(elcImagePickerController*)picker使用info:(NSArray*)info完成了PickingMedi

我正在我的应用程序中使用,我不想将所选的全屏图像保存到我的阵列中,因为如果我选择了40张iPad图像,那就不好了

我想从
UIImagePickerControllerReferenceURL
获取数据,而不是从
UIImagePickerControllerOriginalImage
方法的目录中获取数据
-(void)elcImagePickerController:(elcImagePickerController*)picker使用info:(NSArray*)info完成了PickingMediaWithInfo:(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字节。我尝试了论坛上所有的答案,但没有用


有人能回答这个问题吗?

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

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

UIImagePickerControllerReferenceURL
返回
Assets 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]);
}];
注意:iOS 9中不推荐使用AlassetLibrary。

在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

    }
}
}

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

使用的信息字典中提供的UIImagePickerControllerReferenceURL访问照片的正确方法是通过照片工具包

UIImagePickerControllerDelegate
使用照片框架获取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

感谢您的回复…我也尝试过这样做,尽管在我的日志中我得到的长度是0“2012-05-29 18:32:13.536 myapp[1793:707]长度0”它是0,因为UIImagePickerControllerReferenceURL返回资产库的NSURL对象。所以您需要使用资产库从这个url获取图像。As-NSURL*referenceURL=[info-objectForKey:UIImagePickerControllerReferenceURL];AlassetLibrary*library=[[AlassetLibrary alloc]init];[library assetForURL:referenceURL resultBlock:^(ALAsset*资产){…}故障块:^(NSError*错误){…}];值得一提的是,iOS 9不推荐使用该库。我在下面添加了一个额外的答案。@Jessedc,感谢您的通知,我在答案中添加了一个不推荐使用的注释。注意
UIImagePickerController参考URL
在iOS 11.0中也不推荐使用,因此可以使用条件
NSString*infoKey;if(@available(iOS 11,*){infoKey=UIImagePickerControllerPHAsset;}else{infoKey=UIImagePickerControllerReferenceURL;}
PHAsset.fetchAssetsWithALAssetURLs现在也不推荐使用。