How do I get the data from UIImagePickerControllerReferenceURL?

前端 未结 3 1845
臣服心动
臣服心动 2020-12-08 06:28

I am using ELCImagePickerController in my app and I don\'t want to save the selected fullScreenImage to my array because if i selected 40 iPad images then that is not good.

3条回答
  •  一向
    一向 (楼主)
    2020-12-08 06:37

    UIImagePickerControllerReferenceURL returns NSURL object not the string object. Please change your code to -

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

    UIImagePickerControllerReferenceURL returns NSURL object for Assets Library, so you can get image as -

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

    Note: ALAssetsLibrary is deprecated in iOS 9.

提交回复
热议问题