如何避免 AlAssetLibrary 中的定位服务?我可以在不使用定位服务的情况下使用 AlAssetLibrary 检索文件吗?

发布于 2024-11-02 11:14:49 字数 126 浏览 11 评论 0原文

我创建了一个应用程序来使用 ALAssetLibrary 从 iPhone 照片文件夹中获取图像。 我可以在不使用定位服务的情况下使用 AlAssetLibrary 检索文件吗? 如何避免 AlAssetLibrary 中的定位服务?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2024-11-09 11:14:49

目前,如果不使用位置服务,则无法访问 ALAssetLibrary。你必须使用更有限的 UIImagePickerController 来解决这个问题。

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.

养猫人 2024-11-09 11:14:49

如果您只需要库中的一张图像,则上述答案是不正确的。例如,如果您让用户选择要上传的照片。在这种情况下,您可以使用 ALAssetLibrary 获取单个图像,而无需位置权限。

为此,请使用 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);
                     }
                 }];
}

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);
                     }
                 }];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文