Swift中CGContextRef畫圖用法

Graphics Context是圖形上下文,可以將其理解為一塊畫布,我們可以在上面進(jìn)行繪畫操作,繪制完成后,將畫布放到我們的view中顯示即可,view我們可以把它看作是一個(gè)畫框.下面是我學(xué)習(xí)的時(shí)候小Demo,給大家分享一下,希望對(duì)大家在這方便學(xué)習(xí)有所幫助,具體實(shí)現(xiàn)過程我就直接貼出代碼, 代碼中我寫了注釋一目了然.下面我們先看部分的效果圖:

效果圖_1.png

代碼實(shí)現(xiàn):
① 文字

 let magentaColor = UIColor.greenColor()
    let myString:NSString = "我的劍就是你的劍"
    let helveticaBold = UIFont.init(name: "HelveticaNeue-Bold", size: 30.0);
    myString.drawAtPoint(CGPoint(x: 20, y: 100), withAttributes:[NSFontAttributeName: helveticaBold!,  NSForegroundColorAttributeName: magentaColor])

② 直線

 // 獲取畫布
    let context = UIGraphicsGetCurrentContext()
    // 線條顏色
    CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor)
    // 設(shè)置線條平滑模狭,不需要兩邊像素寬
    CGContextSetShouldAntialias(context, false)
    // 設(shè)置線條寬度
    CGContextSetLineWidth(context, 2.0)
    // 設(shè)置線條起點(diǎn)
    CGContextMoveToPoint(context, 30, 200)
    // 線條結(jié)束點(diǎn)
    CGContextAddLineToPoint(context, 150, 200)
    // 開始畫線
    CGContextStrokePath(context)

③ 矩形

///  無框的
    // 獲取畫布
    let context = UIGraphicsGetCurrentContext()
    // 設(shè)置矩形填充顏色: 藍(lán)色
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0)
    //填充矩形
    CGContextFillRect(context, CGRect(x: 30, y: 260, width: 100, height: 60))
    //執(zhí)行繪畫
    CGContextStrokePath(context);
    
    /// 有框的
    // 矩形填充顏色:紅色
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0)
    // 填充矩形
    CGContextFillRect(context, CGRect(x: 30, y: 350, width: 100, height: 60))
    // 畫筆顏色:黑色
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 1)
    // 畫筆線條粗細(xì)
    CGContextSetLineWidth(context, 1.0)
    // 畫矩形邊框
    CGContextAddRect(context,CGRect(x: 30, y: 350, width: 100, height: 60))
    // 執(zhí)行繪畫
    CGContextStrokePath(context)

④ 圓

// 獲取畫布
    let context = UIGraphicsGetCurrentContext()
    // 畫圓
    CGContextAddEllipseInRect(context, CGRectMake(50,420,100,100))
    // 畫筆顏色
    CGContextSetRGBStrokeColor(context, 0.3, 0.7, 0, 1.0)
    // 關(guān)閉路徑
    CGContextStrokePath(context)

⑤ 扇形與橢圓

// 獲取畫布
    let context = UIGraphicsGetCurrentContext()
    
    //畫扇形蒙秒,也就畫圓,只不過是設(shè)置角度的大小,形成一個(gè)扇形
    CGContextSetFillColorWithColor(context, UIColor.brownColor().CGColor)//填充顏色
    //以10為半徑圍繞圓心畫指定角度扇形
    CGContextMoveToPoint(context, 160, 180)
    CGContextAddArc(context, 180, 180, 30, 120, 200, 2)
    CGContextClosePath(context);
    CGContextDrawPath(context, CGPathDrawingMode.FillStroke) //繪制路徑
    
    //畫橢圓
    CGContextAddEllipseInRect(context, CGRectMake(200, 230, 100, 30)) //橢圓
    CGContextDrawPath(context, CGPathDrawingMode.FillStroke)

⑥ 貝塞爾曲線

// 獲取畫布
    let context = UIGraphicsGetCurrentContext()
    // 二次曲線
    CGContextMoveToPoint(context, 240, 380) // 設(shè)置path的起點(diǎn)
    CGContextAddQuadCurveToPoint(context, 200, 300, 240, 420) //設(shè)置貝塞爾曲線的控制點(diǎn)坐標(biāo)和終點(diǎn)坐標(biāo)
    
    CGContextStrokePath(context)
    //三次曲線函數(shù)
    CGContextMoveToPoint(context, 200, 300) //設(shè)置Path的起點(diǎn)
    CGContextAddCurveToPoint(context,250, 280, 250, 400, 280, 300) //設(shè)置貝塞爾曲線的控制點(diǎn)坐標(biāo)和控制點(diǎn)坐標(biāo)終點(diǎn)坐標(biāo)
    CGContextStrokePath(context)

⑦ 圖片添加水印
效果圖:

效果圖_2.png

代碼實(shí)現(xiàn)部分

 override func drawRect(rect: CGRect) {

 imageView.image = UIImage(named:"bg")?
      .waterMarkedImage("學(xué)習(xí)就要學(xué)會(huì)分享", corner: .TopLeft,
                        margin: CGPoint(x: 20, y: 20),
                        waterMarkTextColor: UIColor.blackColor(),
                        waterMarkTextFont: UIFont.systemFontOfSize(20),
                        backgroundColor: UIColor.clearColor())
    imageView.frame = CGRect(x: 100, y: 100, width: 200, height: 300)
    self.addSubview(imageView)
}

// 擴(kuò)展UIImage
extension UIImage{
  
  //水印位置枚舉
  enum WaterMarkCorner{
    case TopLeft
    case TopRight
    case BottomLeft
    case BottomRight
  }
  
  //添加水印方法
  func waterMarkedImage(waterMarkText:String, corner:WaterMarkCorner = .BottomRight,
                        margin:CGPoint = CGPoint(x: 20, y: 20),
                        waterMarkTextColor:UIColor = UIColor.whiteColor(),
                        waterMarkTextFont:UIFont = UIFont.systemFontOfSize(20),
                        backgroundColor:UIColor = UIColor.clearColor()) -> UIImage{
    
    let textAttributes = [NSForegroundColorAttributeName:waterMarkTextColor,
                          NSFontAttributeName:waterMarkTextFont,
                          NSBackgroundColorAttributeName:backgroundColor]
    let textSize = NSString(string: waterMarkText).sizeWithAttributes(textAttributes)
    var textFrame = CGRectMake(0, 0, textSize.width, textSize.height)
    
    let imageSize = self.size
    switch corner{
    case .TopLeft:
      textFrame.origin = margin
    case .TopRight:
      textFrame.origin = CGPoint(x: imageSize.width - textSize.width - margin.x, y: margin.y)
    case .BottomLeft:
      textFrame.origin = CGPoint(x: margin.x, y: imageSize.height - textSize.height - margin.y)
    case .BottomRight:
      textFrame.origin = CGPoint(x: imageSize.width - textSize.width - margin.x,
                                 y: imageSize.height - textSize.height - margin.y)
    }
    
    // 開始給圖片添加文字水印
    UIGraphicsBeginImageContext(imageSize)
    self.drawInRect(CGRectMake(10, 0, imageSize.width, imageSize.height))
    NSString(string: waterMarkText).drawInRect(textFrame, withAttributes: textAttributes)
    
    let waterMarkedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    return waterMarkedImage
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末签赃,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子且轨,更是在濱河造成了極大的恐慌俗冻,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件妄荔,死亡現(xiàn)場離奇詭異泼菌,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)啦租,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門哗伯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人篷角,你說我怎么就攤上這事焊刹。” “怎么了恳蹲?”我有些...
    開封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵虐块,是天一觀的道長。 經(jīng)常有香客問我阱缓,道長非凌,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任荆针,我火速辦了婚禮敞嗡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘航背。我一直安慰自己喉悴,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開白布玖媚。 她就那樣靜靜地躺著箕肃,像睡著了一般。 火紅的嫁衣襯著肌膚如雪今魔。 梳的紋絲不亂的頭發(fā)上勺像,一...
    開封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天障贸,我揣著相機(jī)與錄音,去河邊找鬼吟宦。 笑死篮洁,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的殃姓。 我是一名探鬼主播袁波,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蜗侈!你這毒婦竟也來了篷牌?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤踏幻,失蹤者是張志新(化名)和其女友劉穎枷颊,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體叫倍,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡偷卧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了吆倦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片听诸。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖蚕泽,靈堂內(nèi)的尸體忽然破棺而出晌梨,到底是詐尸還是另有隱情,我是刑警寧澤须妻,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布仔蝌,位于F島的核電站,受9級(jí)特大地震影響荒吏,放射性物質(zhì)發(fā)生泄漏敛惊。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一绰更、第九天 我趴在偏房一處隱蔽的房頂上張望瞧挤。 院中可真熱鬧,春花似錦儡湾、人聲如沸特恬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽癌刽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間显拜,已是汗流浹背衡奥。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留讼油,地道東北人杰赛。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像矮台,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子根时,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

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