开发者

How can i avoid Location service in AlAssetLibrary? Can i retrieve files using AlAssetLibrary without using Location Service?

i created a application to get images from iPhone photo Folder using ALAssetLibrary. Can i retrieve files using AlAssetLibrary without using Location Service开发者_如何转开发? How can i avoid Location service in AlAssetLibrary?


Currently there is no way to access ALAssetLibrary without using location services. You have to use the, much more limited, UIImagePickerController to get around that problem.


The above answer is incorrect if you only need one image from the library. For example, if you are having the user choose a photo to upload. In this case you can get that single image with ALAssetLibrary, without needing Location permissions.

To do this, use a UIImagePickerController to select the picture; you just need the UIImagePickerControllerReferenceURL, which the UIImagePickerController provides.

This has the benefit of giving you access to an unmodified NSData object, which you can then upload.

This is helpful because re-encoding the image later using UIImagePNGRepresentation() or UIImageJPEGRepresentation() can double the size of your file!

To present the picker:

picker = [[UIImagePickerController alloc] init];
[picker setDelegate:self];
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:nil];

To get the image and/or data:

- (void)imagePickerController:(UIImagePickerController *)thePicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{   
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSURL *imageURL = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

    ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];

    [assetLibrary assetForURL:imageURL
                  resultBlock:^(ALAsset *asset) {
                      // get your NSData, UIImage, or whatever here
                     ALAssetRepresentation *rep = [self defaultRepresentation];
                     UIImage *image = [UIImage imageWithCGImage:[rep fullScreenImage]];

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

                     if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
                         UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
                     }
                 }
                 failureBlock:^(NSError *err) {
                     // Something went wrong; get the image the old-fashioned way                            
                     // (You'll need to re-encode the NSData if you ever upload the image)
                     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

                     if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
                         UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
                     }
                 }];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜