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

夙愿已清 提交于 2020-01-11 06:28:13

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/5702505/how-can-i-avoid-location-service-in-alassetlibrary-can-i-retrieve-files-using-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!