iPhone圖形開發(fā)繪圖教程
iPhone圖形開發(fā)繪圖教程是本文要介紹的內(nèi)容,介紹了很多關(guān)于繪圖類的使用,先來看詳細內(nèi)容講解。
1、繪圖總結(jié):
繪圖前設(shè)置:
- CGContextSetRGBFillColor/CGContextSetFillColorWithColor //填充色
- CGContextSetRGBStrokeColor/CGContextSetStrokeColorWithColor //筆顏色
- CGContextSetLineWidth //線寬度
繪圖后設(shè)置:
注: 畫完圖后,必須 先用CGContextStrokePath來描線,即形狀,后用CGContextFillPath來填充形狀內(nèi)的顏色.
2.常見圖形繪制:
- CGContextFillRect/CGContextFillRects
- CGContextFillEllipseInRect
- CGContextAddRect/CGContextAddRects
- CGContextAddEllipseInRect
- CGContextAddLines
- CGContextMoveToPoint
- CGContextAddLineToPoint
3.常見控制方法:
- CGContextSaveGState
- CGContextRestoreGState
4.創(chuàng)建內(nèi)存圖像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.常用函數(shù):
- 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
- // 設(shè)置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;
關(guān)鍵方法:
- CGContextRef context = CGBitmapContextCreate();
- CGContextTranslateCTM();
- CGContextScaleCTM();
- CGImageRef imageRef = CGBitmapContextCreateImage(context);
- CGImageRelease(imageRef);
小結(jié):iPhone圖形開發(fā)繪圖教程的內(nèi)容介紹完了,希望本文對你有所幫助!