iPhone绘图总结

1.绘图总结: 
绘图前设置: 
CGContextSetRGBFillColor/CGContextSetFillColorWithColor          //填充色 
CGContextSetRGBStrokeColor/CGContextSetStrokeColorWithColor           //笔颜色 
CGContextSetLineWidth                           //线宽度 
绘图后设置: 
注:  画完图后,必须 
先用CGContextStrokePath来描线,即形状 
后用CGContextFillPath来填充形状内的颜色. 
2.常见图形绘制: 
CGContextFillRect/CGContextFillRects 
CGContextFillEllipseInRect 
CGContextAddRect/CGContextAddRects 
CGContextAddEllipseInRect 
CGContextAddLines 
CGContextMoveToPoint 
CGContextAddLineToPoint 
3.常见控制方法: 
CGContextSaveGState ->  将当前的graphics的状态加到context栈中
CGContextRestoreGState ->  就是恢复最近保存的graphics的状态
4.创建内存图像context: 
CGBitmapContextCreate       <-----CGContextRlease释放 
CGColorSpaceCreateWithName    (KCGColorSpaceGenericRGB) 
CGColorSpaceRlease 
CGBitmapContextCreateImage()   <-----CGImageRlease 释放. 
eg: 
CGContextRefMyCreateBitmapContext(intpixelsWide,intpixelsHigh) 

CGContextRef    context=NULL; 
CGColorSpaceRefcolorSpace; 
void*          bitmapData; 
int             bitmapByteCount; 
int             bitmapBytesPerRow; 
bitmapBytesPerRow   =(pixelsWide*4); 
bitmapByteCount     =(bitmapBytesPerRow*pixelsHigh); 
colorSpace=CGColorSpaceCreateDeviceRGB(); 
bitmapData=malloc(bitmapByteCount); 
if(bitmapData==NULL) 

fprintf(stderr,"Memorynotallocated!"); 
returnNULL; 

context=CGBitmapContextCreate(bitmapData,    pixelsWide,    pixelsHigh,    8,    bitmapBytesPerRow,    colorSpace,    kCGImageAlphaPremultipliedLast); 
if(context==NULL) 

free(bitmapData); 
fprintf(stderr,"Contextnotcreated!"); 
returnNULL; 

CGColorSpaceRelease(colorSpace); 
returncontext; 

5.图形的变换: 
CGContextTranslateCTM -> 移动context
CGContextRotateCTM -> 旋转context
CGContextScaleCTM -> 缩放context
   6.常用函数: 
 CGRectContainsPoint(); 
CGRectContainsRect(); 
CGRectIntersectsRect(); 
CGRectIntersection(); 
CGPointEqualToPoint(); 
CGSizeEqualToSize(); 
  7.从原图片中取小图. 
CGImageCreateWithImageInRect 
8.屏幕快照: 
#import "QuartzCore/QuartzCore.h" 

UIGraphicsBeginImageContext(yourView.frame.size); 
[[yourView layer] renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage*screenshot =UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
from:http://www.cppblog.com/zhangyuntaoshe/articles/123066.html 

合并两张bit图到一张image的方法 
To graphically merge two images into a new image, you do something like this: 
UIImage *result = nil; 
unsignedchar *data = calloc(1,size.width*size.height*kBytesPerPixel); 
if (data != NULL) { 
// kCGImageAlphaPremultipliedLast 为预记录的#define value 
// 设置context上下文 
CGContextRef context = CGBitmapContextCreate( 
data, size.width, size.height, 8, size.width*kBytesPerPixel, 
CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast); 
if (context != NULL) { 
UIGraphicsPushContext(context); 
//  Image 为下载的背景图片,用于比较context 
CGContextTranslateCTM(context, 0, size.height); 
CGContextScaleCTM(context, 1, -1); 
[image drawInRect:imageRect]; 
[image2 drawInRect:image2Rect]; 
UIGraphicsPopContext(); 
CGImageRef imageRef = CGBitmapContextCreateImage(context); 
if (imageRef != NULL) { 
result = [UIImageimageWithCGImage:imageRef]; 
CGImageRelease(imageRef); 

CGContextRelease(context); 

free(data); 

return result; 

关键方法:  CGContextRef context = CGBitmapContextCreate(); 
CGContextTranslateCTM(); 
CGContextScaleCTM(); 
CGImageRef imageRef = CGBitmapContextCreateImage(context); 
CGImageRelease(imageRef); 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值