1.繪圖總結:
繪圖前設定:
CGContextSetRGBFillColor/CGContextSetFillColorWithColor //填充色
CGContextSetRGBStrokeColor/CGContextSetStrokeColorWithColor //筆顏色
CGContextSetLineWidth //線寬度
繪圖後設定:
注: 畫完圖後,必須
先用CGContextStrokePath來描線,即形狀
後用CGContextFillPath來填充形狀內的顏色.
2.常見圖形繪製:
CGContextFillRect/CGContextFillRects
CGContextFillEllipseInRect
CGContextAddRect/CGContextAddRects
CGContextAddEllipseInRect
CGContextAddLines
CGContextMoveToPoint
CGContextAddLineToPoint
3.常見控制方法:
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;
}
5.圖形的變換:
CGContextTranslateCTM
CGContextRotateCTM
CGContextScaleCTM
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);
from:http://wzn860701.blog.163.com/blog/static/361528062009102502716872/