4

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

有没有人看出什么毛病?

感谢您的帮助,

感性

4

1 回答 1

7

简单的解决方案是您将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");
         }];
    }];
}
于 2012-02-21T00:23:16.987 回答