畫(huà)板(涂鴉)實(shí)現(xiàn) - iOS

畫(huà)板(涂鴉)實(shí)現(xiàn)


設(shè)置面板.png

背景.png

2017.3.7更新

  • 不使用drawRect
  • 內(nèi)存消耗低 (把demo中XIB刪除掉,內(nèi)存大概在15M左右)
//橡皮擦
- (void)setEraseBrush:(HBPath *)path{
    
    UIGraphicsBeginImageContextWithOptions(self.frame.size, false, 0);
    
    [self.drawImage.image drawInRect:self.bounds];
    
    [[UIColor clearColor] set];
    
    path.bezierPath.lineWidth = _lineWidth;
    
    [path.bezierPath strokeWithBlendMode:kCGBlendModeClear alpha:1.0];
    
    [path.bezierPath stroke];
    
    self.drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
}


需求

  • 更換畫(huà)布背景(獲取 拍照 或者 相冊(cè) 的圖像)
  • 具有拍照 截屏保存功能
  • 不同的畫(huà)筆顏色刃跛,線寬
  • 具有撤銷(xiāo) 返回 清屏 擦除功能

思路

主要分三大塊

  • 背景
  • 畫(huà)布
  • 畫(huà)筆的功能界面

1.首先獲取用戶觸摸事件開(kāi)始

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [self getTouchSet:touches];

    HBPath *path = [HBPath pathToPoint:point pathWidth:_lineWidth isEraser:self.ise];

    path.pathColor = _lineColor;
    
    path.imagePath = [NSString stringWithFormat:@"%@.png",[self getTimeString]];
    
    [self.paths addObject:path];

    [self.tempPoints addObject:[HBDrawPoint drawPoint:point]];
    
    if ([self.delegate respondsToSelector:@selector(drawBoard:drawingStatus:model:)]) {
        [self.delegate drawBoard:self drawingStatus:HBDrawingStatusBegin model:nil];
    }
}

HBPath封裝的NSObject對(duì)象

paths:裝有HBPath對(duì)象

#pragma mark - HBPath
@interface HBPath : NSObject

@property (nonatomic, strong) UIColor *pathColor;//畫(huà)筆顏色
@property (nonatomic, assign) CGFloat lineWidth;//線寬
@property (nonatomic, assign) BOOL isEraser;//橡皮擦
@property (nonatomic, assign) HBDrawingShapeType shapType;//繪制樣式
@property (nonatomic, copy) NSString *imagePath;//圖片路徑
@property (nonatomic, strong) UIBezierPath *bezierPath;


+ (instancetype)pathToPoint:(CGPoint)beginPoint pathWidth:(CGFloat)pathWidth isEraser:(BOOL)isEraser;//初始化對(duì)象
- (void)pathLineToPoint:(CGPoint)movePoint WithType:(HBDrawingShapeType)shapeType;//畫(huà)

@end

2.移動(dòng)

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

    CGPoint point = [self getTouchSet:touches];

    HBPath *path = [self.paths lastObject];
    
    [path pathLineToPoint:point WithType:self.shapType];
    
    if (self.ise) {
        [self setEraseBrush:path];
    }else{
        [self.drawView setBrush:path];
    }
    
    [self.tempPoints addObject:[HBDrawPoint drawPoint:point]];
    
    if ([self.delegate respondsToSelector:@selector(drawBoard:drawingStatus:model:)]) {
        [self.delegate drawBoard:self drawingStatus:HBDrawingStatusMove model:nil];
    }
}

3.結(jié)束

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesMoved:touches withEvent:event];
    
    HBPath *path = [self.paths lastObject];
    
    UIImage *image = [self screenshot:self.drawImage];
    
    self.drawImage.image = image;
    
    [self.drawView setBrush:nil];
    
    NSData *imageData = UIImagePNGRepresentation(image);//UIImageJPEGRepresentation(image, 0.4);
    
    NSString *filePath = [ThumbnailPath stringByAppendingPathComponent:path.imagePath];

    BOOL isSave = [NSFileManager hb_saveData:imageData filePath:filePath];
    
    if (isSave) {
        
        NSLog(@"%@", [NSString stringWithFormat:@"保存成功: %@",filePath]);
    }
    HBDrawModel *model = [[HBDrawModel alloc] init];
    model.paintColor = [_lineColor toColorString];
    model.paintSize = @(_lineWidth);
    model.isEraser = [NSNumber numberWithBool:path.isEraser];
    model.pointList = self.tempPoints;
    model.shapType = [NSNumber numberWithInteger:self.shapType];
    
    if ([self.delegate respondsToSelector:@selector(drawBoard:drawingStatus:model:)]) {
        [self.delegate drawBoard:self drawingStatus:HBDrawingStatusEnd model:model];
    }

    //清空
    [self.tempPoints removeAllObjects];

}

其中HBDrawModel對(duì)象的作用是:操作結(jié)束后傳遞給外界操作的參數(shù)

擁有以下屬性:

/**所有點(diǎn)的集合***/
@property (nonatomic, strong) NSArray * pointList;
/**畫(huà)筆的顏色***/
@property (nonatomic, copy) NSString * paintColor;
/**背景圖片***/
@property (nonatomic, copy) NSString * background;
/**動(dòng)作 (返回 前進(jìn) 畫(huà) 改變背景 清屏)默認(rèn)是 Action_playing ***/
@property (nonatomic, copy) NSString * action;
/**畫(huà)筆大小***/
@property (nonatomic, strong) NSNumber * paintSize;
/**設(shè)備分辨率 寬***/
@property (nonatomic, strong) NSNumber * width;
/**設(shè)備分辨率 高***/
@property (nonatomic, strong) NSNumber * hight;
/**是不是橡皮擦***/
@property (nonatomic, strong) NSNumber * isEraser;

4.繪制

- (void)setBrush:(HBPath *)path
{
    CAShapeLayer *shapeLayer = (CAShapeLayer *)self.layer;
    
    shapeLayer.strokeColor = path.pathColor.CGColor;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.lineJoin = kCALineJoinRound;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.lineWidth = path.bezierPath.lineWidth;
    ((CAShapeLayer *)self.layer).path = path.bezierPath.CGPath;
    
    
}

最后強(qiáng)調(diào)一下關(guān)于橡皮擦的注意點(diǎn)挪捕!

和繪制線是一樣的诚卸,區(qū)別在于繪制的時(shí)候加上下面這句代碼。

if (self.isEraser)
[self.bezierPath strokeWithBlendMode:kCGBlendModeClear alpha:1.0];
更新->在最上面

真正的擦除你已經(jīng)畫(huà)的線,跟你畫(huà)布的背景是不是白色纽什,或者其他顏色沒(méi)有關(guān)系措嵌!如果你的背景是圖片躲叼,設(shè)置畫(huà)筆的顏色與畫(huà)布的顏色一致,就不會(huì)奏效了企巢。

當(dāng)然除了上面是使用貝塞爾路徑繪制以外枫慷,你也可以使用上下文去實(shí)現(xiàn),找準(zhǔn)這個(gè)屬性浪规。媽媽再也不用擔(dān)心橡皮擦啦~~~

問(wèn)題

當(dāng)兩個(gè)設(shè)備中或听,一端正在畫(huà),另一端繪制對(duì)方畫(huà)的線笋婿。如果對(duì)方先慢畫(huà)誉裆,后快畫(huà)。怎么在另一端也繪制出這種速度感缸濒?足丢?
如您知道的話,希望在評(píng)論中可以給我一些思路庇配。

解決

在這里感謝 @柯拉Sir @夢(mèng)的森林 提供的思路斩跌。最新的代碼已經(jīng)更新,需要的朋友自取哈捞慌。

如果這個(gè)文章幫到了你耀鸦,一定給我Star哦!

GitHub 歡迎圍觀Pピ琛袖订!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市嗅虏,隨后出現(xiàn)的幾起案子著角,更是在濱河造成了極大的恐慌,老刑警劉巖旋恼,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件吏口,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡冰更,警方通過(guò)查閱死者的電腦和手機(jī)产徊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)蜀细,“玉大人舟铜,你說(shuō)我怎么就攤上這事〉煜危” “怎么了谆刨?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵塘娶,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我痊夭,道長(zhǎng)刁岸,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任她我,我火速辦了婚禮虹曙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘番舆。我一直安慰自己酝碳,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布恨狈。 她就那樣靜靜地躺著疏哗,像睡著了一般。 火紅的嫁衣襯著肌膚如雪禾怠。 梳的紋絲不亂的頭發(fā)上返奉,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天,我揣著相機(jī)與錄音刃宵,去河邊找鬼衡瓶。 笑死,一個(gè)胖子當(dāng)著我的面吹牛牲证,可吹牛的內(nèi)容都是我干的哮针。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼坦袍,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼十厢!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起捂齐,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蛮放,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后奠宜,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體包颁,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年压真,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了娩嚼。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡滴肿,死狀恐怖岳悟,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤贵少,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布呵俏,位于F島的核電站,受9級(jí)特大地震影響滔灶,放射性物質(zhì)發(fā)生泄漏普碎。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一宽气、第九天 我趴在偏房一處隱蔽的房頂上張望随常。 院中可真熱鬧潜沦,春花似錦萄涯、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至争占,卻和暖如春燃逻,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背臂痕。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工伯襟, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人握童。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓姆怪,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親澡绩。 傳聞我的和親對(duì)象是個(gè)殘疾皇子稽揭,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容