繁体   English   中英

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

[英]iOS : Saving from current UIImageView to Photo Album Failed - Unable to save to photo album

我想做一个应用程序,当按下一个按钮时,它会从当前的主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];
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM