IOS开发基础之画板案例软件的开发
生活随笔
收集整理的這篇文章主要介紹了
IOS开发基础之画板案例软件的开发
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS開發基礎之畫板案例軟件的開發
值此元宵佳節,我依然在學習IOS,幾天沒有更新博客了。今天更新了一下。
源碼在我的主頁里面。
info.plist里面加入這樣的代碼,防止截圖的時候有問題
主要源代碼
// // LJView.h // 畫板 // // Created by 魯軍 on 2021/2/24. //#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface LJView : UIView @property(nonatomic,assign)CGFloat lineWidth;@property(nonatomic,strong)UIColor *lineColor; - (void)clear;- (void)back;- (void)eraser; @endNS_ASSUME_NONNULL_END // // LJView.m // 畫板 // // Created by 魯軍 on 2021/2/24. //#import "LJView.h" #import "LJBezierPath.h"@interface LJView ()//@property(nonatomic,strong) LJBezierPath *path; @property(nonatomic,strong)NSMutableArray *paths ; @end@implementation LJView//- (LJBezierPath *)path{ // if(!_path){ // _path = [LJBezierPath bezierPath]; // } // return _path; //}- (NSMutableArray *)paths{if(!_paths){_paths= [NSMutableArray array];}return _paths; }- (void)clear{[self.paths removeAllObjects];[self setNeedsDisplay]; }- (void)back{[self.paths removeLastObject];[self setNeedsDisplay]; }//橡皮 - (void)eraser{self.lineColor = self.backgroundColor;} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{LJBezierPath *path = [LJBezierPath bezierPath];//起點UITouch *t =touches.anyObject;CGPoint p = [t locationInView:t.view];// [self.path moveToPoint:p];[path moveToPoint:p];[path setLineWidth:self.lineWidth];[path setLineColor1:self.lineColor];[self.paths addObject:path];}- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{// LJBezierPath *path = [LJBezierPath bezierPath];//起點UITouch *t =touches.anyObject;CGPoint p = [t locationInView:t.view]; // [self.path addLineToPoint:p];[[self.paths lastObject] addLineToPoint:p];[self setNeedsDisplay];} - (void)drawRect:(CGRect)rect{// [self.path setLineWidth:self.lineWidth];// [self.path stroke];// 渲染for(LJBezierPath *path in self.paths ){[path setLineJoinStyle:kCGLineJoinRound];[path setLineCapStyle:kCGLineCapRound];// [self.lineColor set];[path.lineColor1 set];[path stroke];} }@end // // LJBezierPath.h // 畫板 // // Created by 魯軍 on 2021/2/26. //#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface LJBezierPath : UIBezierPath@property(nonatomic,strong)UIColor *lineColor1;@endNS_ASSUME_NONNULL_END // // LJBezierPath.m // 畫板 // // Created by 魯軍 on 2021/2/26. //#import "LJBezierPath.h"@implementation LJBezierPath@end // // ViewController.m // 畫板 // // Created by 魯軍 on 2021/2/24. //#import "ViewController.h" #import "LJView.h"@interface ViewController () @property (weak, nonatomic) IBOutlet LJView *ljView; @property (weak, nonatomic) IBOutlet UISlider *lineWidthProgress; @property (weak, nonatomic) IBOutlet UIButton *firstBtn;@end@implementation ViewController- (IBAction)clear:(id)sender {[self.ljView clear]; }- (IBAction)back:(id)sender {[self.ljView back]; }- (IBAction)eraser:(id)sender {[self.ljView eraser]; }- (IBAction)save:(id)sender {//1開啟圖片類型的上下文UIGraphicsBeginImageContextWithOptions(self.ljView.bounds.size, NO, 0);//4 獲取當前上下文CGContextRef ctx = UIGraphicsGetCurrentContext();//3 截圖[self.ljView.layer renderInContext:ctx];//5取圖片UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//2.關閉上下文UIGraphicsEndImageContext();//6.保存圖片UIImageWriteToSavedPhotosAlbum(image, NULL, NULL, NULL);} - (IBAction)lineWidthChange:(UISlider *)sender {self.ljView.lineWidth = sender.value;} - (IBAction)lineColorChange:(UIButton *)sender {self.ljView.lineColor = sender.backgroundColor;}- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.ljView.lineWidth = self.lineWidthProgress.value;[self lineColorChange:self.firstBtn];}@end總結
以上是生活随笔為你收集整理的IOS开发基础之画板案例软件的开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序员保持身心健康的八种方式
- 下一篇: IOS开发基础之SQLite3数据库的使