我创建了一个应用程序,使用ALAssetLibrary从iPhone照片文件夹中获取图像。我可以检索文件使用AlAssetLibrary不使用位置服务?我如何避免在AlAssetLibrary位置服务?
目前没有办法访问ALAssetLibrary不使用位置服务。你必须使用更有限的UIImagePickerController来解决这个问题
如果您只需要库中的一张图像,则上述答案是不正确的。例如,如果您让用户选择要上传的照片。在这种情况下,你可以用ALAssetLibrary获得单个图像,而不需要Location权限。
要做到这一点,使用UIImagePickerController来选择图片;你只需要UIImagePickerControllerReferenceURL
,它由UIImagePickerController提供。
这样做的好处是允许您访问未修改的NSData
对象,然后可以上传该对象。
这很有帮助,因为稍后使用UIImagePNGRepresentation()
或UIImageJPEGRepresentation()
重新编码图像可以使文件大小增加一倍!
显示选择器:
picker = [[UIImagePickerController alloc] init];
[picker setDelegate:self];
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:nil];
获取图像和/或数据:
- (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);
}
}];
}