画板使用

Painting.h
#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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值