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