【问题标题】:Image cannot be saved using UIImageWriteToSavedPhotosAlbum to photo album无法使用 UIImageWriteToSavedPhotosAlbum 将图像保存到相册
【发布时间】:2012-02-28 13:34:51
【问题描述】:

我有一个名为“保存到相册”的保存按钮。按钮顶部是小的 UIImageView,用户可以单击下面的按钮将照片的实际大小保存到相册。我的问题是当我点击保存按钮时,它显示一个错误:无法将图像保存到相册。

我认为我做得对,但我的经验太有限了。

H 文件:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController  {

    IBOutlet UIButton *btnReturn;
    IBOutlet UIButton *savePhoto;
    IBOutlet UIImage *saveImage;

}

@property (nonatomic, retain) IBOutlet UIButton *btnReturn;
@property (nonatomic, retain) IBOutlet UIButton *savePhoto;
@property (nonatomic, retain) IBOutlet UIImage *saveImage;

-(IBAction) saveToPhotoAlbum;

@end

下面是我的代码:

-(IBAction) saveToPhotoAlbum{

    NSString *saveMyPhoto=[NSHomeDirectory() stringByAppendingPathComponent:@"sample.png"];

    UIImage *saved=[UIImage imageWithContentsOfFile:saveMyPhoto]; 

    NSData *imageData = [NSData dataWithContentsOfFile:(NSString *)UIImagePNGRepresentation(saved)];
    [imageData writeToFile:(NSString *)saved atomically:YES ];

    UIImageWriteToSavedPhotosAlbum(saved, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    UIAlertView *alert;


    if (error)
        alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                           message:@"Unable to save image to Photo Album." 
                                          delegate:self cancelButtonTitle:@"Ok" 
                                 otherButtonTitles:nil];
    else 
        alert = [[UIAlertView alloc] initWithTitle:@"Success" 
                                           message:@"Image saved to Photo Album." 
                                          delegate:self cancelButtonTitle:@"Ok" 
                                 otherButtonTitles:nil];
    [alert show];
    [alert release];
}

【问题讨论】:

  • 实际错误是什么(即在您的图像中添加NSLog(@"%@", error);:didFinishSavingWithError:contextInfo: 方法。
  • 图片无法保存到相册。程序可以成功运行。只是它无法保存。我的代码有没有可能不正确?
  • 不,这不是错误,这只是您在警报中输入的信息。当您在图像中添加行 NSLog(@"%@", error); 时会发生什么情况:didFinishSavingWithError:contextInfo: 方法 - 应该有一个错误输出到控制台。我们需要知道那是什么。
  • 好的。这是错误。错误域=ALAssetsLibraryErrorDomain 代码=-3304 “未能为保存的照片编码图像。” UserInfo=0x7857630 {NSUnderlyingError=0x7851f20 "无法为保存的照片编码图像。", NSLocalizedDescription=无法为保存的照片编码图像。}
  • 我该怎么办?我只是想成功保存图像。

标签: objective-c xcode4 ios5 ios4 uiimageview


【解决方案1】:

只需创建一个 UIImage 对象并从文件中加载图像。然后只需调用 UIIMageWriteToSavedPhotoAlbum 函数并将 UIImage 对象作为第一个参数传递,然后其余的都可以是 NIL。见Apple SDK documentation.

【讨论】:

    【解决方案2】:

    这些线看起来真的很可疑:

    NSString *saveMyPhoto=[NSHomeDirectory() stringByAppendingPathComponent:@"sample.png"];
    ...
    UIImage *saved = [UIImage imageWithContentsOfFile:saveMyPhoto]; 
    ...
    [imageData writeToFile:(NSString *)saved atomically:YES ];
    

    您希望这能做什么?你得到一个图像的路径,加载它然后再次保存它,但是你已经将一个 UIImage 转换为一个 NSString 并希望它会神奇地变成磁盘上的有效路径?

    至于你问的问题,你确定你的图片是一张有效的图片吗?

    【讨论】:

    • 我只是想让它成功保存到相册。就这样。是的,我的图片是 png 格式。关于我的代码有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2015-01-16
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多