标签:
在继承UIView 创建PIDrawerView类,.m中申明一个枚举类型NS_ENUM,set 与get 画线类型DrawingMode和颜色设置UIColor
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, DrawingMode) {
DrawingModeNone = 0,
DrawingModePaint,
DrawingModeErase,
};
@interface PIDrawerView : UIView
@property (nonatomic, readwrite) DrawingMode drawingMode;
@property (nonatomic, strong) UIColor *selectedColor;
@end
在在继承UIView.h文件中实现申明
#import "PIDrawerView.h"
@interface PIDrawerView ()
{
CGPoint previousPoint;
CGPoint currentPoint;
}
@property (nonatomic, strong) UIImage * viewImage;
@end
@implementation PIDrawerView
- (void)awakeFromNib
{
[self initialize];
}
- (void)drawRect:(CGRect)rect
{
[self.viewImage drawInRect:self.bounds];
}
#pragma mark - setter methods
- (void)setDrawingMode:(DrawingMode)drawingMode
{
_drawingMode = drawingMode;
}
#pragma mark - Private methods
- (void)initialize
{
currentPoint = CGPointMake(0, 0);
previousPoint = currentPoint;
_drawingMode = DrawingModeNone;
_selectedColor = [UIColor blackColor];
}
- (void)eraseLine
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.viewImage drawInRect:self.bounds];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), previousPoint.x, previousPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
previousPoint = currentPoint;
[self setNeedsDisplay];
}
- (void)drawLineNew
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.viewImage drawInRect:self.bounds];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.selectedColor.CGColor);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), previousPoint.x, previousPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
previousPoint = currentPoint;
[self setNeedsDisplay];
}
- (void)handleTouches
{
if (self.drawingMode == DrawingModeNone) {
// do nothing
}
else if (self.drawingMode == DrawingModePaint) {
[self drawLineNew];
}
else
{
[self eraseLine];
}
}
#pragma mark - Touches methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self];
previousPoint = p;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
currentPoint = [[touches anyObject] locationInView:self];
[self handleTouches];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
currentPoint = [[touches anyObject] locationInView:self];
[self handleTouches];
}
在ViewController中引进 PIDrawerView类和MobileCoreServices/MobileCoreServices库文件
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "PIDrawerView.h"
@interface ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
PIDrawerView * viewdr;
UIView * viewimageView;
}
@property (nonatomic, strong) UIColor *selectedColor;
@property(nonatomic)UIImageView * backGroundImageView;
@end
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
viewimageView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 20, 350)];
[self.view addSubview:viewimageView];
_backGroundImageView = [[UIImageView alloc]initWithFrame:viewimageView.bounds];
_backGroundImageView.userInteractionEnabled = YES;
viewdr = [[PIDrawerView alloc]initWithFrame:viewimageView.bounds];
viewdr.userInteractionEnabled = YES;
viewdr.backgroundColor = [UIColor clearColor];
[viewimageView addSubview:_backGroundImageView];
[viewimageView addSubview:viewdr];
self.selectedColor = [UIColor blackColor];
// 开始画线
[viewdr setSelectedColor:self.selectedColor];
[viewdr setSelectedColor:self.selectedColor];
UIView * viewHight = [[UIView alloc]initWithFrame:CGRectMake(1, self.view.frame.size.height - 50, self.view.frame.size.width, 100)];
[self.view addSubview:viewHight];
UIButton * buttonq = [[UIButton alloc]initWithFrame:CGRectMake(20, 0, 150, 50)];
[buttonq setTitle:@"编辑涂抹" forState:UIControlStateNormal];
[buttonq addTarget:self action:@selector(onLick2) forControlEvents:UIControlEventTouchUpInside];
[buttonq setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[viewHight addSubview:buttonq];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(150, 0, 150, 50)];
[button setTitle:@"进入相册" forState:UIControlStateNormal];
[button addTarget:self action:@selector(onLick) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[viewHight addSubview:button];
UIButton * buttonsave = [[UIButton alloc]initWithFrame:CGRectMake(350, 0, 50, 50)];
[buttonsave setTitle:@"截屏" forState:UIControlStateNormal];
[buttonsave setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[buttonsave addTarget:self action:@selector(saveOnLick) forControlEvents:UIControlEventTouchUpInside];
[viewHight addSubview:buttonsave];
}
//保存
-(void)saveOnLick{
// 开始截屏
UIGraphicsBeginImageContextWithOptions(viewimageView.bounds.size, NO, 2.0);
// 路径
CGContextRef context = UIGraphicsGetCurrentContext();
//
[self.view.layer renderInContext:context];
// 图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
if (error == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil ];
[alert show];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil ];
[alert show];
}
}
//进行划线
-(void)onLick2{
[viewdr setDrawingMode:DrawingModePaint];
}
//进入相册
-(void)onLick{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Pick" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Pick From Library",nil];
[actionSheet showInView:self.view];
}
#pragma mark - UIActionSheetDelegate methods
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
mediaUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeSavedPhotosAlbum];
mediaUI.allowsEditing = NO;
mediaUI.delegate = self;
[self presentViewController:mediaUI animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = nil;
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
else
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo)
{
image = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
}
}
self.backGroundImageView.image = image;
[picker dismissViewControllerAnimated:YES completion:nil];
}
UIActionSheet--从相册去出照片进行涂抹,截图保存到相册的demo完成
标签:
原文地址:http://www.cnblogs.com/xingxiafei/p/5104388.html