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
本文介绍如何通过继承UIView并重写drawRect方法实现自定义绘图,同时提供了基于CALayer创建位图上下文的方法。文章包含具体代码实现细节。
1万+

被折叠的 条评论
为什么被折叠?



