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

前端 未结 2 1563
一整个雨季
一整个雨季 2021-01-14 14:29

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 a

相关标签:
2条回答
  • 2021-01-14 14:51

    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.

    0 讨论(0)
  • 2021-01-14 14:54

    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 讨论(0)
提交回复
热议问题