最近老畫圓,所以對CGContextAddArc的接觸比較多。
CGContextAddArc(Context, CGFloat x , CGFloat y, CGFloat radius, CGFloat startAngle , CGFloat endAngle, int clockwise);
這個方法牛人門都很熟悉了,我只是記錄我自己的。
關於角度,由x,y可確定圓心坐標,而0,0角度位於圓心的正下方。
startAngle是由0,0偏移的。
偏移方向是由clockwise控制的,0為順時針,1為逆時針。
iPhone中雙緩沖的實現
原理不復雜:
1. 創建一個bitmap context
2. 在bitmap context上畫東西
3. 通過bitmap context生成一個CGImage
4. 通過UIImage把CGImage內容畫到屏幕上
示例代碼:
// create the bitmap context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil,27,27,8,0, colorSpace,kCGImageAlphaPremultipliedLast);
CFRelease(colorSpace);
// create an arrow image
// set the fill color
CGColorRef fillColor = [[UIColor blackColor] CGColor];
CGContextSetFillColor(context, CGColorGetComponents(fillColor));
CGContextBeginPath(context);
CGContextMoveToPoint(context, 8.0f, 13.0f);
CGContextAddLineToPoint(context, 24.0f, 4.0f);
CGContextAddLineToPoint(context, 24.0f, 22.0f);
CGContextClosePath(context);
CGContextFillPath(context);
// convert the context into a CGImageRef
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage* image2 = [UIImage imageWithCGImage:image];
[image2 drawInRect:CGRectMake(0, 0, 120, 160)];
其中對context繪圖的部分可以用來畫耗時的3d動畫等。