- (IBAction)save:(id)sender {
//1.把画板东西生成一张图片保存
UIGraphicsBeginImageContextWithOptions(self.drawView.bounds.size, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.drawView.layer renderInContext:ctx];
//生成一张图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文.
UIGraphicsEndImageContext();
//写到系统相册当中
// UIImageWriteToSavedPhotosAlbum 这个方法,默认保存到系统相机胶卷,但是@selector后面的方法 必须是这种格式: - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
//保存图片到系统相册
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
NSLog(@"保存成功");
}
//选择照片
- (IBAction)chosePic:(id)sender {
//系统相册控制器
UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];
//设置照片的来源
pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//设置代理
pickVC.delegate = self;
//modal出系统相册控制器
[self presentViewController:pickVC animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//获取当前选中的图片.通过UIImagePickerControllerOriginalImage就能获取.
UIImage *image = info[UIImagePickerControllerOriginalImage];
HandleImageView *handView = [[HandleImageView alloc] init];
handView.frame = self.drawView.bounds;
handView.image = image;
handView.delegate = self;
NSLog(@"%@", handView.backgroundColor);
[self.drawView addSubview:handView];
// self.drawView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
网友评论