iphone 繪圖小結

來源:互聯網
上載者:User

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/

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.