文章出處

  在iOS中使用Quartz畫圖時,第一步就是要獲取畫布(圖形上下文),然后再畫布上做各種操作。先看下CoreGraphics.h這個頭文件,就可以知道能夠創建多少種上下文類型。

#include <CoreGraphics/CGBase.h>

#include <CoreGraphics/CGAffineTransform.h>
#include <CoreGraphics/CGBitmapContext.h>
#include <CoreGraphics/CGColor.h>
#include <CoreGraphics/CGColorSpace.h>
#include <CoreGraphics/CGContext.h>
#include <CoreGraphics/CGDataConsumer.h>
#include <CoreGraphics/CGDataProvider.h>
#include <CoreGraphics/CGError.h>
#include <CoreGraphics/CGFont.h>
#include <CoreGraphics/CGFunction.h>
#include <CoreGraphics/CGGeometry.h>
#include <CoreGraphics/CGGradient.h>
#include <CoreGraphics/CGImage.h>
#include <CoreGraphics/CGLayer.h>
#include <CoreGraphics/CGPDFArray.h>
#include <CoreGraphics/CGPDFContentStream.h>
#include <CoreGraphics/CGPDFContext.h>
#include <CoreGraphics/CGPDFDictionary.h>
#include <CoreGraphics/CGPDFDocument.h>
#include <CoreGraphics/CGPDFObject.h>
#include <CoreGraphics/CGPDFOperatorTable.h>
#include <CoreGraphics/CGPDFPage.h>
#include <CoreGraphics/CGPDFScanner.h>
#include <CoreGraphics/CGPDFStream.h>
#include <CoreGraphics/CGPDFString.h>
#include <CoreGraphics/CGPath.h>
#include <CoreGraphics/CGPattern.h>
#include <CoreGraphics/CGShading.h>

  可以看到,可以創建的上下文包括:  

//圖片
CGContextRef CGBitmapContextCreate
//layer(UIView)
CGContextRef CGLayerGetContext
//pdf類型
CGContextRef CGPDFContextCreate

  我們再看下UIGraphics.h這個頭文件,里面已經提供這3種上下文創建的封裝給UIKit使用的方法,在普通的情況下,使用UIGraphics中的方法即可,而且畫布已經做了坐標的翻轉。

 

 

UIKIT_EXTERN CGContextRef UIGraphicsGetCurrentContext(void) CF_RETURNS_NOT_RETAINED;

表示是個弱引用類型,直接使用,不用釋放。

        UIGraphicsBeginImageContext(self.bounds.size);
        CGContextRef context=UIGraphicsGetCurrentContext();

        CGContextSetRGBFillColor(context, 1, 0, 0, 1);
        CGContextSetRGBStrokeColor(context, 0, 1, 0, 1);
        CGContextFillRect(context, CGRectMake(0, 100, 100, 100));
        NSString *text=@"文字";
        UIFont *font=[UIFont systemFontOfSize:14];
        [text drawAtPoint:CGPointMake(0, 200) withAttributes:font.fontDescriptor.fontAttributes];
        UIImage *img=[UIImage imageNamed:@"gg.jpg"];
        [img drawInRect:CGRectMake(0, 300, 100, 100)];
        CGImageRef cgimg = CGBitmapContextCreateImage(context);
        UIImage *resultImg = [UIImage imageWithCGImage:cgimg];
        CGImageRelease(cgimg);

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()