IOS開發——繪圖

原文地址:http://blog.csdn.net/pjk1129/article/details/7161383 html


一、繪圖總結:app

繪圖前設置:ide

  
  
  
  
  1. CGContextSetRGBFillColor/CGContextSetFillColorWithColor  //填充色    函數

  2. CGContextSetRGBStrokeColor/CGContextSetStrokeColorWithColor //筆顏色    this

  3. CGContextSetLineWidth   //線寬度   spa

繪圖後設置:.net

注:  畫完圖後,必須 先用CGContextStrokePath來描線,即形狀,後用CGContextFillPath來填充形狀內的顏色.xml

2.常見圖形繪製:htm

  
  
  
  
  1. CGContextFillRect/CGContextFillRects    blog

  2. CGContextFillEllipseInRect    

  3. CGContextAddRect/CGContextAddRects    

  4. CGContextAddEllipseInRect    

  5. CGContextAddLines    

  6. CGContextMoveToPoint    

  7. CGContextAddLineToPoint  

3.常見控制方法:

  
  
  
  
  1. CGContextSaveGState    

  2. CGContextRestoreGState  

4.建立內存圖像context:

  
  
  
  
  1. CGBitmapContextCreate       <-----CGContextRlease釋放    

  2. CGColorSpaceCreateWithName    (KCGColorSpaceGenericRGB)    

  3. CGColorSpaceRlease    

  4. CGBitmapContextCreateImage()   <-----CGImageRlease 釋放.    

  5. eg:    

  6. CGContextRefMyCreateBitmapContext(intpixelsWide,intpixelsHigh)    

  7. {    

  8. CGContextRef    context=NULL;    

  9. CGColorSpaceRefcolorSpace;    

  10. void*          bitmapData;    

  11. int             bitmapByteCount;    

  12. int             bitmapBytesPerRow;    

  13. bitmapBytesPerRow   =(pixelsWide*4);    

  14. bitmapByteCount     =(bitmapBytesPerRow*pixelsHigh);    

  15. colorSpace=CGColorSpaceCreateDeviceRGB();    

  16. bitmapData=malloc(bitmapByteCount);    

  17. if(bitmapData==NULL)    

  18. {    

  19. fprintf(stderr,"Memorynotallocated!");    

  20. returnNULL;    

  21. }    

  22. context=CGBitmapContextCreate(bitmapData,    

  23. pixelsWide,    pixelsHigh,    8,    

  24. bitmapBytesPerRow,    colorSpace,    

  25. kCGImageAlphaPremultipliedLast);    

  26. if(context==NULL)    

  27. {    

  28. free(bitmapData);    

  29. fprintf(stderr,"Contextnotcreated!");    

  30. returnNULL;    

  31. }    

  32. CGColorSpaceRelease(colorSpace);    

  33. returncontext;    

  34. }  

5.圖形的變換:

  
  
  
  
  1. CGContextTranslateCTM    

  2. CGContextRotateCTM    

  3. CGContextScaleCTM  

6.經常使用函數:

  
  
  
  
  1.  CGRectContainsPoint();    

  2. CGRectContainsRect();    

  3. CGRectIntersectsRect();    

  4. CGRectIntersection();    

  5. CGPointEqualToPoint();    

  6. CGSizeEqualToSize();  

7.從原圖片中取小圖.

  
  
  
  
  1. CGImageCreateWithImageInRect  

8.屏幕快照:

  
  
  
  
  1. #import "QuartzCore/QuartzCore.h"    


  2. UIGraphicsBeginImageContext(yourView.frame.size);    

  3. [[yourView layer] renderInContext:UIGraphicsGetCurrentContext()];    

  4. UIImage*screenshot =UIGraphicsGetImageFromCurrentImageContext();    

  5. UIGraphicsEndImageContext();    

  6. from:http://www.cppblog.com/zhangyuntaoshe/articles/123066.html  

合併兩張bit圖到一張p_w_picpath的方法

  
  
  
  
  1. To graphically merge two p_w_picpaths into a new p_w_picpath, you do something like this:    

  2. UIImage *result = nil;    

  3. unsignedchar *data = calloc(1,size.width*size.height*kBytesPerPixel);    

  4. if (data != NULL) {    

  5. // kCGImageAlphaPremultipliedLast 爲預記錄的#define value    

  6. // 設置context上下文    

  7. CGContextRef context = CGBitmapContextCreate(    

  8. data, size.width, size.height, 8, size.width*kBytesPerPixel,    

  9. CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);    

  10. if (context != NULL) {    

  11. UIGraphicsPushContext(context);    

  12. //  Image 爲下載的背景圖片,用於比較context    

  13. CGContextTranslateCTM(context, 0, size.height);    

  14. CGContextScaleCTM(context, 1, -1);    

  15. [p_w_picpath drawInRect:p_w_picpathRect];    

  16. [p_w_picpath2 drawInRect:p_w_picpath2Rect];    

  17. UIGraphicsPopContext();    

  18. CGImageRef p_w_picpathRef = CGBitmapContextCreateImage(context);    

  19. if (p_w_picpathRef != NULL) {    

  20. result = [UIImagep_w_picpathWithCGImage:p_w_picpathRef];    

  21. CGImageRelease(p_w_picpathRef);    

  22. }    

  23. CGContextRelease(context);    

  24. }    

  25. free(data);    

  26. }    

  27. return result;  

關鍵方法:

  
  
  
  
  1. CGContextRef context = CGBitmapContextCreate();    

  2. CGContextTranslateCTM();    

  3. CGContextScaleCTM();    

  4. CGImageRef p_w_picpathRef = CGBitmapContextCreateImage(context);    

  5. CGImageRelease(p_w_picpathRef);  

相關文章
相關標籤/搜索