Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

objective c - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...