美文网首页iOS开发
绘画--画板(作业)

绘画--画板(作业)

作者: 金牛忘忧 | 来源:发表于2016-07-22 08:46 被阅读0次

//

//ViewController.m

//绘画--画板(作业)

//

#import"ViewController.h"

#import"DrawView.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width

#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interfaceViewController()

{

DrawView*drawView;

UIImage* sendImg;

}

@property(weak,nonatomic)IBOutletUIButton*clearButton;

@property(weak,nonatomic)IBOutletUIButton*undoButton;

@property(weak,nonatomic)IBOutletUIButton*eraserButton;

@property(weak,nonatomic)IBOutletUISlider*widthSlider;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

drawView= [[DrawViewalloc]initWithFrame:CGRectMake(0,70,kScreenWidth,kScreenHeight-190)];

drawView.backgroundColor= [UIColorgrayColor];

[self.viewaddSubview:drawView];

//NSLog(@"%@",NSHomeDirectory());获得路径是使用

}

- (IBAction)clearButtonAction:(UIButton*)sender {

[drawView.linesremoveAllObjects];

[drawViewsetNeedsDisplay];

}

- (IBAction)undoButtonAction:(UIButton*)sender {

[drawView.linesremoveLastObject];

[drawViewsetNeedsDisplay];

}

- (IBAction)eraserButtonAction:(UIButton*)sender {

drawView.color= [UIColorgrayColor];

drawView.lineWidth=15;

}

- (IBAction)saveButtonAction:(UIButton*)sender {

[selfselectView];

//将新图片写到桌面上

NSString*filePath =@"/Users/wuxujian/Desktop/mm.png";

NSData*imgData =UIImageJPEGRepresentation(sendImg,1);

[imgDatawriteToFile:filePathatomically:YES];

//存储到相册

UIImageWriteToSavedPhotosAlbum(sendImg,self,@selector(image:didFinishSavingWithError:contextInfo:),nil);

}

- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo;{

NSLog(@"保存图片成功");

}

//截取图片

-(UIImage*)selectView{

UIGraphicsBeginImageContext(drawView.bounds.size);

[drawView.layerrenderInContext:UIGraphicsGetCurrentContext()];

UIImage*viewImg =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndPDFContext();

CGImageRefimageRef = viewImg.CGImage;

CGRectrect =CGRectMake(drawView.frame.origin.x,drawView.frame.origin.y,drawView.frame.size.width,drawView.frame.size.height);

CGImageRefrefImgRef =CGImageCreateWithImageInRect(imageRef, rect);

sendImg= [[UIImagealloc]initWithCGImage:refImgRef];

returnsendImg;

}

- (IBAction)greenButtonAction:(UIButton*)sender {

drawView.color=[UIColorgreenColor];

}

- (IBAction)redButtonAction:(UIButton*)sender {

drawView.color=[UIColorredColor];

}

- (IBAction)blueButtonAction:(UIButton*)sender {

drawView.color=[UIColorblueColor];

}

- (IBAction)widthSliderAction:(UISlider*)sender {

drawView.lineWidth=_widthSlider.value/_widthSlider.maximumValue*10+1;

}

@end

//LineModel.h

//绘画--画板(作业)

//

#import

#import

#import

@interfaceLineModel :NSObject

@property(assign,nonatomic)CGMutablePathRefpath;//路径

//颜色

@property(strong,nonatomic)UIColor*color;

//宽度

@property(assign,nonatomic)CGFloatwidth;

@end

//LineModel.m

//绘画--画板(作业)

//

#import"LineModel.h"

@implementationLineModel

-(void)setPath:(CGMutablePathRef)path{

if(_path!= path) {

CGPathRelease(_path);

CGPathRetain(path);

_path= path;

}

}

- (void)dealloc {

CGPathRelease(_path);

}

@end

//DrawView.h

//绘画--画板(作业)

//

#import

@interfaceDrawView :UIView

@property(strong,nonatomic)UIColor*color;

@property(assign,nonatomic)CGFloatlineWidth;

//使用一个可变数组,来管理所有的线

@property(strong,nonatomic)NSMutableArray*lines;

@end

//DrawView.m

//绘画--画板(作业)

//

#import"DrawView.h"

#import"LineModel.h"

@interfaceDrawView()

{

CGMutablePathRef_linePath;

LineModel*line;

////使用一个可变数组,来管理所有的线

//NSMutableArray *_lines;

}

@end

@implementationDrawView

- (instancetype)initWithFrame:(CGRect)frame {

self= [superinitWithFrame:frame];

if(self) {

self.backgroundColor= [UIColorwhiteColor];

_lines= [NSMutableArrayarray];

_color= [UIColorblackColor];

_lineWidth=3;

}

returnself;

}

//1记录手指移动的轨迹

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event {

//创建路径

_linePath=CGPathCreateMutable();

//设置起始点

UITouch*touch = [touchesanyObject];

CGPointlocation = [touchlocationInView:self];

CGPathMoveToPoint(_linePath,NULL, location.x, location.y);

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event {

//向路径中添加点

UITouch*touch = [touchesanyObject];

CGPointlocation = [touchlocationInView:self];

CGPathAddLineToPoint(_linePath,NULL, location.x, location.y);

[selfsetNeedsDisplay];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event {

//将当前画完的线条加入到数组中去

line= [[LineModelalloc]init];

line.path=_linePath;

line.color=_color;

line.width=_lineWidth;

[_linesaddObject:line];

CGPathRelease(_linePath);

_linePath=NULL;

//绘制线条

[selfsetNeedsDisplay];

}

//2绘制图形界面

- (void)drawRect:(CGRect)rect {

//获取图形上下文

CGContextRefcontext =UIGraphicsGetCurrentContext();

//绘制数组中储存的线条

for(LineModel*lineModelin_lines) {

CGContextSetLineWidth(context, lineModel.width);

[lineModel.colorsetStroke];

//绘制路径

CGContextAddPath(context, lineModel.path);

CGContextDrawPath(context,kCGPathStroke);

}

//全局变量中储存的线条

if(_linePath==NULL) {

return;

}

//绘制路径

CGContextAddPath(context,_linePath);

CGContextDrawPath(context,kCGPathStroke);

}

@end

相关文章

  • 绘画--画板(作业)

    // //ViewController.m //绘画--画板(作业) // #import"ViewControl...

  • 插画头像插画板绘教程 老师教你绘画思维

    插画头像插画板绘教程 老师教你绘画思维插画头像插画板绘教程 老师教你绘画思维插画头像插画板绘教程 老师教你绘画思维...

  • 无标题文章

    会合Yui绘画板

  • SAI 2.0简体中文版软件+安装教程

    SAI一直以来都是许多插画师首选的绘画工具,这款软件兼容几乎所有型号的绘画板,这款软件兼容几乎所有型号的绘画板,通...

  • 第二步7.素描工具:画板和画架介绍

    素描工具:画板和画架介绍 画架画架【an easel; a drawing stand 】是绘画时用来放置画板的架...

  • 绘画板隐私政策

    绘画板 非常重视用户隐私的保护。本隐私政策适用于绘画板 iOS 客户端(iPad和iPhone)的产品和服务。请您...

  • 入账绘画板

    因为是写日记的,所以排版晒出来,内容就没写!这是我花了一天绘画出来的,是人物与植物间隔,圆框用蓝描,里面用土色,代...

  • iOS绘画板

    今天给大家讲讲如何用代码实现绘图功能,虽然是很简陋的绘图功能,但也值得大家一看.先看看效果图吧. 从这副图中大家可...

  • 自学画画第5天

    就当做稍微休息一下吧,最近考虑买绘画板。

  • 2019-04-20

    哥哥给我买了一个新玩具,这个玩具是充上电才能用的。这个玩具叫绘画板,有电了在绘画板可以画很多小动物。我可以画好多的...

网友评论

    本文标题:绘画--画板(作业)

    本文链接:https://www.haomeiwen.com/subject/rxcijttx.html