iOS:从当前UIImageView保存到相册失败 - 无法保存到相册

时间:2012-02-27 16:00:17

标签: objective-c ios xcode

我想做一个应用程序,当按下按钮时,从当前主UIImageView保存图像。将其保存到相册。

我的程序所做的是在我按下保存到相册按钮后显示错误。可能我的代码结构不对。或者有人能告诉我正确的代码结构吗?

到目前为止,这是我的进展。

-(IBAction) saveToPhotoAlbum{

NSString *saveMyPhoto=[NSHomeDirectory() stringByAppendingPathComponent:@"image.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];
}

1 个答案:

答案 0 :(得分:0)

您应该提供保存图像的正确路径。一个也是读写的,请查看Documents目录NSString * docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"],然后使用相同的方法-stringByAppendingPathComponent添加文件名:或者只创建一个包含两个内容的格式化字符串