iOS-保存图片到相册

 //保存
 
    UIButton *saveBtn = [[UIButton alloc] init];
    //    saveBtn.frame = CGRectMake((screenWidth-270)/4+1, 550/9*screenScale + 272/9*screenScale + 128/9*screenScale + 172/9*screenScale + 55,133, 42);
    saveBtn.frame = CGRectMake((screenWidth-270)/4+1,1766/3,133, 42);
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn setBackgroundColor:[UIColor colorWithRed:58/256.0 green:150/256.0 blue:253/256.0 alpha:1.0f]];
    [saveBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:saveBtn];

-(void)save
{
    NSLog(@"---save");
    //    //如果你的系统大于等于7.0 >
//     if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
//    
//         UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"" message:@"保存二维码到相册" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
//         alertView.tag = 1;
//         [alertView show];
//    
//    }else{
//    
//        //>=iOS8以上才有alertController这个方法
//        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"保存二维码到相册" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//            UIImageWriteToSavedPhotosAlbum(_codeView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
//        }];
//        
//        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//            
//        }];
//        
//        [alertController addAction:cancelAction];
//        [alertController addAction: okAction];
//        [self presentViewController:alertController animated:YES completion:nil];
//    
//}

   UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"" message:@"保存二维码到相册" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alertView.tag = 1;
    [alertView show];


}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    
    if (error == nil) {
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
        alertView.tag = 2;
        [alertView show];
        
    }else{
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"请去设置-隐私-照片打开允许访问" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
         alertView.tag = 3;
        [alertView show];
    }
    
}

#pragma mark uialertview deleagate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 0 && alertView.tag ==1) {
        
        UIImageWriteToSavedPhotosAlbum(_codeView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }else if(buttonIndex == 0 && alertView.tag==3){
     
    //打开设置-隐私-照片
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    
    }

}

原文地址:https://www.cnblogs.com/linxiu-0925/p/5322018.html