画板使用
程序员文章站
2022-03-11 20:11:59
...
Painting.h
Painting.m
#import <UIKit/UIKit.h> //CONSTANTS: #define kRubberWidth 20 #define kBrushLineAlpha 1.0 #define kPaintViewBackGroudImg @"背景.png" @interface Painting : UIView { UIImageView *drawImage; int mouseMoved; BOOL mouseSwiped; BOOL isRubber; CGPoint lastPoint; CGFloat kBrushRGBColorRed; CGFloat kBrushRGBColorGreen; CGFloat kBrushRGBColorBlue; CGFloat kBrushLineWidth; } @property(nonatomic, readwrite) BOOL isRubber; @property(nonatomic, readwrite) CGFloat kBrushLineWidth; - (void)clear; - (void)save; //- (void)changRGBColorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue; - (void)setBrushColorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue; @end
Painting.m
#import "Painting.h" #import <QuartzCore/QuartzCore.h> @implementation Painting @synthesize isRubber; @synthesize kBrushLineWidth; - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { } return self; } - (void)drawRect:(CGRect)rect { drawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kPaintViewBackGroudImg]]; drawImage.frame = self.frame; [self addSubview:backGroudImage]; [self addSubview:drawImage]; mouseMoved = 0; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { mouseSwiped = NO; UITouch *touch = [touches anyObject]; //双击清空 //if ([touch tapCount] == 2) { // [self clear]; //} lastPoint = [touch locationInView:self]; //lastPoint.y -= 20; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { mouseSwiped = YES; UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self]; //currentPoint.y -= 20; // only for 'kCGLineCapRound' UIGraphicsBeginImageContext(self.frame.size); //Albert Renshaw - Apps4Life [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound CGContextSetLineWidth(UIGraphicsGetCurrentContext(), kBrushLineWidth); // for size 线条宽度 CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), kBrushRGBColorRed, kBrushRGBColorGreen, kBrushRGBColorBlue, kBrushLineAlpha); //values for R, G, B, and Alpha //CGContextSetLineJoin(UIGraphicsGetCurrentContext() , kCGLineJoinRound ); if (isRubber) { CGContextSetLineWidth(UIGraphicsGetCurrentContext(), kRubberWidth); // for size 线条宽度 CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);//混合模式 // CGContextClearRect(UIGraphicsGetCurrentContext(),CGRectMake(currentPoint.x - kRubberWidth/2, currentPoint.y - kRubberWidth/2,kRubberWidth,kRubberWidth)); } // else { // CGContextBeginPath(UIGraphicsGetCurrentContext()); // CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); // CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); // } CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); lastPoint = currentPoint; mouseMoved++; if (mouseMoved == 10) { mouseMoved = 0; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //UITouch *touch = [touches anyObject]; //双击清空 //if ([touch tapCount] == 2) { // [self clear]; //} if(!mouseSwiped) { UIGraphicsBeginImageContext(self.frame.size); [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound CGContextSetLineWidth(UIGraphicsGetCurrentContext(), kBrushLineWidth); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), kBrushRGBColorRed, kBrushRGBColorGreen, kBrushRGBColorBlue, kBrushLineAlpha); //CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); if (isRubber) { CGContextClearRect(UIGraphicsGetCurrentContext(),CGRectMake(lastPoint.x - kRubberWidth/2, lastPoint.y - kRubberWidth/2,kRubberWidth,kRubberWidth)); }else { CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); } CGContextStrokePath(UIGraphicsGetCurrentContext()); CGContextFlush(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } } - (void) clear{ [drawImage setImage:[UIImage imageNamed:kPaintViewBackGroudImg]]; } - (void)save{ UIGraphicsBeginImageContext(self.bounds.size); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); //UIImage *viewImage = [UIImage imageNamed:@"pink2.png"]; UIGraphicsEndImageContext(); if (viewImage != nil) { UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存完毕" message:@"保存到图片浏览目录中" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil]; [alert show]; [alert release]; } } - (void)setBrushColorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue { kBrushRGBColorRed = red; kBrushRGBColorGreen = green; kBrushRGBColorBlue = blue; } - (void)dealloc { [super dealloc]; } @end
上一篇: 人的一生最后悔什么