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 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| //保存 - (IBAction)save:(UIBarButtonItem *)sender { //把画板截屏 //1-开启上下文 UIGraphicsBeginImageContextWithOptions(_paintView.bounds.size, NO, 0.0); //获取当前上下文 CGContextRef ctx=UIGraphicsGetCurrentContext(); //把画板上的内容渲染到上下文 [_paintView.layer renderInContext:ctx]; //获取新的图片 UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext(); //关闭上下文 UIGraphicsEndImageContext(); //保存到用户的相册里面 UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } //保存相册后回调 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { if (error) { //保存失败 MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeAnnularDeterminate; hud.label.text = @"保存失败"; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [MBProgressHUD hideHUDForView:self.view animated:YES]; }); //[MBProgressHUD :@"保存失败"]; NSLog(@"保存失败"); }else{ //保存成功 MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeAnnularDeterminate; hud.label.text = @"保存成功"; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [MBProgressHUD hideHUDForView:self.view animated:YES]; }); NSLog(@"保存成功"); } }
|