【问题标题】:How to save and reload image using ALAsset?如何使用 ALAsset 保存和重新加载图像?
【发布时间】:2012-02-09 08:04:27
【问题描述】:

我正在编写一个应用程序,让用户可以拍摄照片并将它们分配给设备。为此,我需要能够存储拍摄图片的 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]);
                }];
}

有人看出有什么问题吗?

感谢您的帮助,

感应

【问题讨论】:

  • 我仍在寻找有好主意的人来帮助我我仍然被这个愚蠢的问题所困扰。其实这不可能这么难。有很多应用程序在使用照片库,不是吗?

标签: ios image xcode4 camera alassetslibrary


【解决方案1】:

简单的解决方案是您将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");
         }];
    }];
}

【讨论】:

  • 好吧,实际上我决定将图片保存到应用程序特定的目录中,现在可以正常工作了。尽管如此,我还是尝试了您的代码。我能够保存图像 - 这从来都不是问题。问题是重新加载它...我无法收到图像的缩略图...您能否显示加载图像的代码?
  • 简单地替换 NSLog("we have our ALAsset!"); with UIImage *image = [UIImage imageWithCGImage:[asset defaultRepresentation].fullResolutionImage];
  • 嗯,听起来很简单......我以为我已经尝试过了,但很可能我做了其他事情......不过,谢谢你的帮助!
  • @AnthonyMcCormick,什么是 AssetLibraryUtil?使用时出现“使用未声明的标识符”错误。
  • @T.J.它只是我用来获取对 AssetLibrary 的单例引用的工具,我已将上面的代码替换为仅引用 ALAssetsLibrary。
猜你喜欢
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-11
  • 2020-01-07
相关资源
最近更新 更多