我创建了一个使用ALAssetLibrary从iPhone照片文件夹获取图像的应用程序。我可以不使用位置服务而使用AlAssetLibrary检索文件吗?如何避免使用AlAssetLibrary中的位置服务?
发布于 2011-04-19 11:17:19
目前,如果不使用定位服务,就无法访问ALAssetLibrary。你必须使用更有限的UIImagePickerController来解决这个问题。
发布于 2013-02-07 09:27:05
如果您只需要库中的一个镜像,则上述答案是不正确的。例如,如果您让用户选择要上传的照片。在这种情况下,您可以使用ALAssetLibrary获得该单个图像,而不需要位置权限。
为此,使用UIImagePickerController选择图片;您只需要UIImagePickerController提供的UIImagePickerControllerReferenceURL。
这样做的好处是让您可以访问未经修改的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);
}
}];
}https://stackoverflow.com/questions/5702505
复制相似问题