iPhone重绘机制drawRect

iPhone重绘机制drawRect


如何使用iPhone进行绘图、重绘操作
iPhone的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩展UIView的类,并重写drawRect方法,在这里进行绘图操作,程序会自动调用此方法进行绘图。


下面先说明一下绘图,比如,你想绘制一个方块,你需要写一个类来扩展UIView并在drawRect方法中填入如下代码:


- (void)drawRect:(CGRect)rect {  

 
  // Drawing code. 
 
  //获得处理的上下文   
 
  CGContextRef context =UIGraphicsGetCurrentContext();   
 
  //设置线条样式   
 
  CGContextSetLineCap(context,kCGLineCapSquare);    
 
  //设置线条粗细宽度   
 
 CGContextSetLineWidth(context, 1.0);    
 
  
 
  //设置颜色   
 
 CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);    
 
  //开始一个起始路径   
 
  CGContextBeginPath(context);    
 
 //起始点设置为(0,0):注意这是上下文对应区域中的相对坐标,   
 
  CGContextMoveToPoint(context,0, 0);    
 
  //设置下一个坐标点   
 
 CGContextAddLineToPoint(context, 100, 100);    
 
  //设置下一个坐标点   
 
 CGContextAddLineToPoint(context, 0, 150);   
 
  //设置下一个坐标点   
 
 CGContextAddLineToPoint(context, 50, 180);   
 
  //连接上面定义的坐标点   
 
  CGContextStrokePath(context); 
 
   
}  



再说明一下重绘,重绘操作仍然在drawRect方法中完成,但是苹果不建议直接调用drawRect方法,当然如果你强直直接调用此方法,当然是没有效果的。苹果要求我们调用UIView类中的setNeedsDisplay方法,则程序会自动调用drawRect方法进行重绘。(调用setNeedsDisplay会自动调用drawRect)


在UIView中,重写drawRect: (CGRect)aRect方法,可以自己定义想要画的图案.且此方法一般情况下只会画一次.也就是说这个drawRect方法一般情况下只会被掉用一次. 

当某些情况下想要手动重画这个View,只需要掉用[self setNeedsDisplay]方法即可.
drawRect掉用是在Controller->loadView,Controller->viewDidLoad两方法之后掉用的.所以不用担心在控制器中,这些View的drawRect就开始画了.这样可以在控制器中设置一些值给View(如果这些Viewdraw的时候需要用到某些变量值).


1.如果在UIView初始化时没有设置rect大小,将直接导致drawRect不被自动调用。
2.该方法在调用sizeThatFits后被调用,所以可以先调用sizeToFit计算出size。然后系统自动调用drawRect:方法。
3.通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。
4.直接调用setNeedsDisplay,或者setNeedsDisplayInRect:触发drawRect:,但是有个前提条件是rect不能为0.
以上1,2推荐;而3,4不提倡




1、若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate的ref并且不能用于画图。drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay或者 setNeedsDisplayInRect ,让系统自动调该方法。
2、若使用calayer绘图,只能在drawInContext:中(类似鱼drawRect)绘制,或者在delegate中的相应方法绘制。同样也是调用setNeedDisplay等间接调用以上方法。

3、若要实时画图,不能使用gestureRecognizer,只能使用touchbegan等方法来掉用setNeedsDisplay实时刷新屏幕


转自:http://blog.sina.com.cn/s/blog_726dee1101015gs8.html





  本文标签:iPhone 图形 绘图 


  iPhone图形开发绘图教程是本文要介绍的内容,介绍了很多关于绘图类的使用,先来看详细内容讲解  。

  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);

  小结:iPhone图形开发绘图教程的内容介绍完



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值