swift 在圖片上根據點數組劃線

可直接調用此方法,傳入imageView和點數組。

func drawLineInImageWithImage(imageView:UIImageView,points:[CGPoint]){
        UIGraphicsBeginImageContext(imageView.frame.size);
        imageView.image?.drawInRect(CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height));
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), CGLineCap.Round);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
     CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), true);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), self.lineColor.r(),self.lineColor.g(),self.lineColor.b(),self.lineColor.a());
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        let begin = points[0];
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), begin.x,begin.y);//起點坐標
        for point in points{
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x, point.y);
        }
        //終點坐標
        CGContextStrokePath(UIGraphicsGetCurrentContext());
    imageView.image=UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

圖片 1.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 點擊查看原文 Web SDK 開發手冊 SDK 概述 網易云信 SDK 為 Web 應用提供一個完善的 IM 系統...
    layjoy閱讀 13,944評論 0 15
  • 第5章 引用類型(返回首頁) 本章內容 使用對象 創建并操作數組 理解基本的JavaScript類型 使用基本類型...
    大學一百閱讀 3,270評論 0 4
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,969評論 19 139
  • 2017.02.22 可以練習,每當這個時候,腦袋就犯困,我這腦袋真是神奇呀,一說讓你做事情,你就犯困,你可不要太...
    Carden閱讀 1,378評論 0 1
  • 百花聊賴敗芬芳 秋風不屈送水流 一杯濁酒灌心腸 生死無懼淡憂愁
    崔凌辰閱讀 137評論 0 0