iOS View保存到相册

  • 将View以及子View合成Image后保存
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    - (void)saveImageAction {
    //此处的CGSizeMake是根据需要制定截取图片的宽、高;NO/YES表示是否透明
    UIGraphicsBeginImageContextWithOptions(self.cardImg.bounds.size, NO, 0.0); //NO,YES 控制是否透明
    [self.cardImg.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // 生成后的image
    [self saveImageToPhotos:image];
    }

    - (void)saveImageToPhotos:(UIImage*)savedImage {
    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }

    - (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
    NSString *msg = nil ;
    if(error != NULL){
    msg = @"保存图片失败" ;
    }else{
    msg = @"保存图片成功" ;
    }
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存结果" message:msg preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *ture = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alertController addAction:ture];
    [self presentViewController:alertController animated:YES completion:^{

    }];
    }
本文总阅读量