Quartz2D绘图

来源:互联网 发布:linux yum的rpm安装包 编辑:程序博客网 时间:2024/05/16 06:33

1.在视图中绘图
在视图的- (void)drawRect:(NSRect)dirtyRect方法中获取图形上下文,进行绘图。
- (void)drawRect:(NSRect)dirtyRect
{
     // Drawing code here.
    CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];//取得Graphics Context

    CGContextSetRGBFillColor(myContext, 1, 0, 0, 1);//设置填充色
    CGContextFillRect(myContext, CGRectMake(0, 0, 200, 100));//画矩形
    CGContextSetRGBFillColor(myContext, 0, 0, 1, .5);//设置填充色
    CGContextFillRect(myContext, CGRectMake(0, 0, 100, 200));//画矩形
}


2.在pdf中绘图
Quartz 2D API提供了两个函数来创建PDFGraphics Context
(1)CGPDFContextCreateWithURL:当你需要用Core Foundation URL指定pdf输出的位置时使用该函数。
CGContextRef MyPDFContextCreate(const CGRect * inMediaBox, CFStringRef path)
{
    CGContextRef myOutContext = NULL;
    CFURLRef url;
    url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, false);

    if(url != NULL) {
        myOutContext = CGPDFContextCreateWithURL(url, inMediaBox, NULL);
        CFRelease(url);
    }
    
    return myOutContext;
}
(2)CGPDFContextCreate:当需要将pdf输出发送给数据用户时使用该方法。
CGContextRef MyPDFContextCreate2(const CGRect * inMediaBox, CFStringRef path) {
    CGContextRef myOutContext = NULL;
    CFURLRef url;
    CGDataConsumerRef dataConsumer;
    
    url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, false);
    
    if(url != NULL) {
        dataConsumer = CGDataConsumerCreateWithURL(url);
        if(dataConsumer != NULL) {
            myOutContext = CGPDFContextCreate(dataConsumer, inMediaBox, NULL);
            CGDataConsumerRelease(dataConsumer);
        }
        CFRelease(url);
    }
    
    return myOutContext;
}

在pdf中绘图
-(void)drawPDF {
    CGRect mediaBox;
    CGFloat myPageWidth = 400;
    CGFloat myPageHeight = 500;
    mediaBox = CGRectMake(0, 0, myPageWidth, myPageHeight);
    CGContextRef myPDFContext = MyPDFContextCreate(&mediaBox, CFSTR("/Users/pengxc/test.pdf"));
    
    CFStringRef myKeys[1];
    CFTypeRef myValues[1];
    
    myKeys[0] = kCGPDFContextMediaBox;
    myValues[0] = (CFTypeRef)CFDataCreate(NULL, (const UInt8 *)&mediaBox, sizeof(CGRect));
    
    CFDictionaryRef pageDictionary = CFDictionaryCreate(NULL, (const void **)myKeys, (const void **)myValues, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    
    CGPDFContextBeginPage(myPDFContext, pageDictionary);
    CGContextSetRGBFillColor(myPDFContext, 1, 0, 0, 1);
    CGContextFillRect(myPDFContext, CGRectMake(0, 0, 200, 100));
    CGContextSetRGBFillColor(myPDFContext, 0, 0, 1, .5);
    CGContextFillRect(myPDFContext, CGRectMake(0, 0, 100, 200));
    
    CGPDFContextEndPage(myPDFContext);
    
    CFRelease(pageDictionary);
    CFRelease(myValues[0]);
    CGContextRelease(myPDFContext);
}

3.在位图内存中绘图
创建位图GraphicsContext
CGContextRef MyCreateBitmapContext(int pixelsWide, int pixelsHigh) {
    CGContextRef context = NULL;
    CGColorSpaceRef colorSpace;
    void * bitmapData;
    int bitmapByteCount;
    int bitmapBytesPerRow;
   
    bitmapBytesPerRow = (pixelsWide * 4);
    bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
    colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    bitmapData = malloc(bitmapByteCount);
    
    if(bitmapData == NULL) {
        fprintf(stderr, "Memory not allocated!");
        return NULL;
    }
    
    context = CGBitmapContextCreate(bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
    
    if(context == NULL) {
        free(bitmapData);
        fprintf(stderr, "Context not created!");
        return NULL;
    }
    
    CGColorSpaceRelease(colorSpace);
    return context;
}
在位图图形上下文中绘图
-(CGImageRef) myDrawBitmap {
    CGContextRef myBitmapContext = MyCreateBitmapContext(200, 200);
    
    CGContextSetRGBFillColor(myBitmapContext, 1, 0, 0, 1);
    CGContextFillRect(myBitmapContext, CGRectMake(0, 0, 200, 100));
    CGContextSetRGBFillColor(myBitmapContext, 0, 0, 1, .5);
    CGContextFillRect(myBitmapContext, CGRectMake(0, 0, 100, 200));
   
    //将所绘的图形放到一个CGImageRef对象中,返回给调用者
    CGImageRef myImage = CGBitmapContextCreateImage(myBitmapContext);
    
    char * bitmapData = CGBitmapContextGetData(myBitmapContext);
    CGContextRelease(myBitmapContext);
    if(bitmapData) free(bitmapData);

    return myImage;//caller should call CGImageRelease(myImage)
}










原创粉丝点击