Iphone 我如何避免图书馆的定位服务?我可以在不使用位置服务的情况下使用AlAssetLibrary检索文件吗?
我创建了一个应用程序,使用ALAssetLibrary从iPhone照片文件夹中获取图像。 我可以在不使用位置服务的情况下使用AlAssetLibrary检索文件吗?Iphone 我如何避免图书馆的定位服务?我可以在不使用位置服务的情况下使用AlAssetLibrary检索文件吗?,iphone,objective-c,xcode,libraries,alasset,Iphone,Objective C,Xcode,Libraries,Alasset,我创建了一个应用程序,使用ALAssetLibrary从iPhone照片文件夹中获取图像。 我可以在不使用位置服务的情况下使用AlAssetLibrary检索文件吗? 如何避免在AlAssetLibrary中使用定位服务?目前,如果不使用定位服务,就无法访问AlAssetLibrary。您必须使用更有限的UIImagePickerController来解决这个问题。如果您只需要库中的一个图像,则上述答案是不正确的。例如,如果您让用户选择要上载的照片。在这种情况下,您可以使用ALAssetLibr
如何避免在AlAssetLibrary中使用定位服务?目前,如果不使用定位服务,就无法访问AlAssetLibrary。您必须使用更有限的UIImagePickerController来解决这个问题。如果您只需要库中的一个图像,则上述答案是不正确的。例如,如果您让用户选择要上载的照片。在这种情况下,您可以使用ALAssetLibrary获取单个映像,而无需位置权限 为此,请使用UIImagePickerController选择图片;您只需要UIImagePickerController提供的
UIImagePickerController参考URL
这样做的好处是,您可以访问未修改的NSData
对象,然后可以上载该对象
这很有帮助,因为稍后使用UIImagePNGRepresentation()
或uiimagejpegresentation()
重新编码图像会使文件大小加倍
要呈现选择器,请执行以下操作:
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);
}
}];
}
还是这样吗(2012年2月)?@BradSmith请看下面我的答案,不确定是否有用。