ios – 如何使用ALAsset保存和重新加载图像?

ios – 如何使用ALAsset保存和重新加载图像?,第1张

概述我正在编写一个应用程序,让用户可以拍摄照片并将其分配给设备.为此,我需要能够存储拍摄的图片的URL并在之后重新加载图片. 我知道有过几次讨论,但很奇怪,没有一个Ansers帮助过我…… 所以这是我保存图像的代码: - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInf 我正在编写一个应用程序,让用户可以拍摄照片并将其分配给设备.为此,我需要能够存储拍摄的图片的URL并在之后重新加载图片.

我知道有过几次讨论,但很奇怪,没有一个Ansers帮助过我……

所以这是我保存图像的代码:

- (voID) imagePickerController:(UIImagePickerController *)picker dIDFinishPickingMediawithInfo:(NSDictionary *)info{    ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init];    UIImage * image;    // Request to save Image    if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) {        image = [info objectForKey:UIImagePickerControllerEditedImage];        [library writeImagetoSavedPhotosAlbum:[image CGImage] orIEntation:(ALAssetorIEntation)[image imageOrIEntation] completionBlock:^(NSURL * assertURL,NSError * error){            if (error) {                UIAlertVIEw * alert = [[UIAlertVIEw alloc] initWithTitle:NSLocalizedString(@"PictureSaveErrorTitle",@"Title if saving picture fails")                                                                  message:NSLocalizedString(@"PictureSaveErrorMessage",@"Message if saving picture fails")                                                                 delegate:nil                                                        cancelbuttonTitle:NSLocalizedString(@"PictureSaveErrorOK",@"OK if saving picture fails")                                                        otherbuttonTitles:nil];                [alert show];            } else {                [self setUserImage:[assertURL absoluteString]];                [imageOfDevice setimage:image];            }        }];    } else {        image = [info objectForKey:UIImagePickerControllerOriginalimage];        [self setUserImage:[[info valueForKey:UIImagePickerControllerReferenceURL] path]];    }    [self dismissModalVIEwControllerAnimated:YES];}

这是用于重新加载图像:

if (userImage != nil) {        ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init];        [library assetForURL:[NSURL fileURLWithPath:userImage]                  resultBlock:^(ALAsset * asset){                     [imageOfDevice setimage:[UIImage imageWithCGImage:[asset thumbnail]]];                 }                failureBlock:^(NSError * error){                    NSLog(@"cannot get image - %@",[error localizedDescription]);                }];}

有人看到有什么错吗?

谢谢你的帮助,

sensslen

解决方法 简单的解决方案是您传递的是absoluteString而不是NSUrl.

这个代码适用于我内部选择器完成.

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera ){    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalimage];    [library writeImagetoSavedPhotosAlbum:image.CGImage orIEntation:(ALAssetorIEntation)image.imageOrIEntation completionBlock:^(NSURL *assetURL,NSError *error )    {        NSLog(@"IMAGE SAVED TO PHOTO ALBUM");        [library assetForURL:assetURL resultBlock:^(ALAsset *asset )        {            NSLog(@"we have our ALAsset!");        }                 failureBlock:^(NSError *error )         {             NSLog(@"Error loading asset");         }];    }];}
总结

以上是内存溢出为你收集整理的ios – 如何使用ALAsset保存和重新加载图像?全部内容,希望文章能够帮你解决ios – 如何使用ALAsset保存和重新加载图像?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1056065.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存