Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the data from UIImagePickerControllerReferenceURL?

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.

I want to get data from UIImagePickerControllerReferenceURL instead of UIImagePickerControllerOriginalImage from the dict of method - (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info.

I have tried:

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

However, every time I am getting 0 bytes. I have tried with all the answers available in forum but no use.

Can anyone answer this please?

like image 773
Steve Gear Avatar asked May 29 '12 12:05

Steve Gear


3 Answers

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.

like image 161
saadnib Avatar answered Oct 13 '22 20:10

saadnib


This question ranks well on Google for UIImagePickerControllerReferenceURL so I thought I'd add the correct way to use UIImagePickerControllerReferenceURL in iOS9 and later as ALAssetLibrary has been deprecated in favour of the Photos Framework.

The correct way to access the photo using UIImagePickerControllerReferenceURL provided in the info dictionary from imagePickerController(_:didFinishPickingMediaWithInfo:) is via Photos Kit PHAsset.

A basic implementation of UIImagePickerControllerDelegate utilising Photos Framework to fetch the UIImage would look something like this:

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

The code above will not process callbacks when sourceType is .Camera since as the info dictionary does not contain UIImagePickerControllerReferenceURL.

like image 10
Jessedc Avatar answered Oct 13 '22 19:10

Jessedc


in ELCImagePickers "Selected assets" you can do

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

    }
}

Then in your other class to save from array

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

    }
}
}
like image 1
mrburns05 Avatar answered Oct 13 '22 20:10

mrburns05