獲取CGContextRef的幾種方法

1.繼承UIView,重寫drawRect方法

CGContextRef ctx = UIGraphicsGetCurrentContext();

2.根據創建好的CALayer獲取當前Layer的Context

- (CGContextRef) MyCreateBitmapContext:(CALayer*)layer
{
    int pixelsWide = layer.bounds.size.width;
    int pixelsHigh = layer.bounds.size.height;
    CGContextRef    context = NULL;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;
    
    bitmapBytesPerRow   = (pixelsWide * 4);// 1
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate (NULL,// 4
                                     pixelsWide,
                                     pixelsHigh,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);
    if (context== NULL)
    {
        return NULL;
    }
    CGColorSpaceRelease( colorSpace );// 6
    
    return context;// 7
}

可供擴展:

http://www.cocoabuilder.com/archive/cocoa/218456-drawing-an-nsimage-in-calayer.html

http://www.cocoachina.com/bbs/read.php?tid-43635.html

http://www.freeboxgame.com/freebox/technologyCampQuestion.aspx?questionid=56

http://www.cocoachina.com/bbs/m/read.php?tid=51704#read

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章