原文地址:http://blog.csdn.net/pjk1129/article/details/7161383 html
一、繪圖總結:app
繪圖前設置:ide
CGContextSetRGBFillColor/CGContextSetFillColorWithColor //填充色 函數 CGContextSetRGBStrokeColor/CGContextSetStrokeColorWithColor //筆顏色 this CGContextSetLineWidth //線寬度 spa
CGContextSetRGBFillColor/CGContextSetFillColorWithColor //填充色 函數
CGContextSetRGBStrokeColor/CGContextSetStrokeColorWithColor //筆顏色 this
CGContextSetLineWidth //線寬度 spa
繪圖後設置:.net
注: 畫完圖後,必須 先用CGContextStrokePath來描線,即形狀,後用CGContextFillPath來填充形狀內的顏色.xml
2.常見圖形繪製:htm
CGContextFillRect/CGContextFillRects blog CGContextFillEllipseInRect CGContextAddRect/CGContextAddRects CGContextAddEllipseInRect CGContextAddLines CGContextMoveToPoint CGContextAddLineToPoint
CGContextFillRect/CGContextFillRects blog
CGContextFillEllipseInRect
CGContextAddRect/CGContextAddRects
CGContextAddEllipseInRect
CGContextAddLines
CGContextMoveToPoint
CGContextAddLineToPoint
3.常見控制方法:
CGContextSaveGState CGContextRestoreGState
CGContextSaveGState
CGContextRestoreGState
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; }
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!");
CGColorSpaceRelease(colorSpace);
returncontext;
5.圖形的變換:
CGContextTranslateCTM CGContextRotateCTM CGContextScaleCTM
CGContextTranslateCTM
CGContextRotateCTM
CGContextScaleCTM
6.經常使用函數:
CGRectContainsPoint(); CGRectContainsRect(); CGRectIntersectsRect(); CGRectIntersection(); CGPointEqualToPoint(); CGSizeEqualToSize();
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
#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圖到一張p_w_picpath的方法
To graphically merge two p_w_picpaths into a new p_w_picpath, 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); [p_w_picpath drawInRect:p_w_picpathRect]; [p_w_picpath2 drawInRect:p_w_picpath2Rect]; UIGraphicsPopContext(); CGImageRef p_w_picpathRef = CGBitmapContextCreateImage(context); if (p_w_picpathRef != NULL) { result = [UIImagep_w_picpathWithCGImage:p_w_picpathRef]; CGImageRelease(p_w_picpathRef); } CGContextRelease(context); } free(data); } return result;
To graphically merge two p_w_picpaths into a new p_w_picpath, 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);
[p_w_picpath drawInRect:p_w_picpathRect];
[p_w_picpath2 drawInRect:p_w_picpath2Rect];
UIGraphicsPopContext();
CGImageRef p_w_picpathRef = CGBitmapContextCreateImage(context);
if (p_w_picpathRef != NULL) {
result = [UIImagep_w_picpathWithCGImage:p_w_picpathRef];
CGImageRelease(p_w_picpathRef);
CGContextRelease(context);
free(data);
return result;
關鍵方法:
CGContextRef context = CGBitmapContextCreate(); CGContextTranslateCTM(); CGContextScaleCTM(); CGImageRef p_w_picpathRef = CGBitmapContextCreateImage(context); CGImageRelease(p_w_picpathRef);
CGContextRef context = CGBitmapContextCreate();
CGContextTranslateCTM();
CGContextScaleCTM();