IOS Quartz 各种绘制图形用法

  1. - (void)drawRect:(CGRect)rect  
  2. {  
  3.     CGContextRef context = UIGraphicsGetCurrentContext();  
  4.        
  5.    
  6.        
  7.     /*NO.1画一条线 
  8.        
  9.      CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色 
  10.      CGContextMoveToPoint(context, 20, 20); 
  11.      CGContextAddLineToPoint(context, 200,20); 
  12.      CGContextStrokePath(context); 
  13.     */  
  14.    
  15.        
  16.        
  17.     /*NO.2写文字 
  18.        
  19.     CGContextSetLineWidth(context, 1.0); 
  20.     CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5); 
  21.     UIFont  *font = [UIFont boldSystemFontOfSize:18.0]; 
  22.     [@"公司:公司\n部门:ERP事业部\n姓名:McLiang" drawInRect:CGRectMake(20, 40, 280, 300) withFont:font]; 
  23.     */  
  24.    
  25.        
  26.     /*NO.3画一个正方形图形 没有边框 
  27.   
  28.     CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5); 
  29.     CGContextFillRect(context, CGRectMake(2, 2, 270, 270)); 
  30.     CGContextStrokePath(context); 
  31.     */  
  32.     
  33.        
  34.     /*NO.4画正方形边框 
  35.       
  36.     CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色 
  37.     CGContextSetLineWidth(context, 2.0); 
  38.     CGContextAddRect(context, CGRectMake(2, 2, 270, 270)); 
  39.     CGContextStrokePath(context); 
  40.     */  
  41.    
  42.        
  43.     /*NO.5画方形背景颜色 
  44.        
  45.     CGContextTranslateCTM(context, 0.0f, self.bounds.size.height); 
  46.     CGContextScaleCTM(context, 1.0f, -1.0f); 
  47.     UIGraphicsPushContext(context); 
  48.     CGContextSetLineWidth(context,320); 
  49.     CGContextSetRGBStrokeColor(context, 250.0/255, 250.0/255, 210.0/255, 1.0); 
  50.     CGContextStrokeRect(context, CGRectMake(0, 0, 320, 460)); 
  51.     UIGraphicsPopContext(); 
  52.     */  
  53.    
  54.     /*NO.6椭圆 
  55.        
  56.      CGRect aRect= CGRectMake(80, 80, 160, 100); 
  57.      CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0); 
  58.      CGContextSetLineWidth(context, 3.0); 
  59.      CGContextAddEllipseInRect(context, aRect); //椭圆 
  60.      CGContextDrawPath(context, kCGPathStroke); 
  61.     */  
  62.    
  63.     /*NO.7 
  64.     CGContextBeginPath(context); 
  65.     CGContextSetRGBStrokeColor(context, 0, 0, 1, 1); 
  66.     CGContextMoveToPoint(context, 100, 100); 
  67.     CGContextAddArcToPoint(context, 50, 100, 50, 150, 50); 
  68.     CGContextStrokePath(context); 
  69.     */  
  70.    
  71.     /*NO.8渐变 
  72.     CGContextClip(context); 
  73.     CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); 
  74.     CGFloat colors[] = 
  75.     { 
  76.         204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00, 
  77.         29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 1.00, 
  78.         0.0 / 255.0,  50.0 / 255.0, 126.0 / 255.0, 1.00, 
  79.     }; 
  80.     CGGradientRef gradient = CGGradientCreateWithColorComponents 
  81.     (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4)); 
  82.     CGColorSpaceRelease(rgb); 
  83.     CGContextDrawLinearGradient(context, gradient,CGPointMake 
  84.                                 (0.0,0.0) ,CGPointMake(0.0,self.frame.size.height), 
  85.                                 kCGGradientDrawsBeforeStartLocation); 
  86.      */  
  87.        
  88.       
  89.     /* NO.9四条线画一个正方形 
  90.     //画线 
  91.         UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0]; 
  92.     CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0); 
  93.        CGContextSetFillColorWithColor(context, aColor.CGColor); 
  94.     CGContextSetLineWidth(context, 4.0); 
  95.     CGPoint aPoints[5]; 
  96.     aPoints[0] =CGPointMake(60, 60); 
  97.     aPoints[1] =CGPointMake(260, 60); 
  98.     aPoints[2] =CGPointMake(260, 300); 
  99.     aPoints[3] =CGPointMake(60, 300); 
  100.     aPoints[4] =CGPointMake(60, 60); 
  101.     CGContextAddLines(context, aPoints, 5); 
  102.     CGContextDrawPath(context, kCGPathStroke); //开始画线 
  103.      */  
  104.        
  105.        
  106.        
  107.     /*  NO.10 
  108.     UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0]; 
  109.     CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0); 
  110.     CGContextSetFillColorWithColor(context, aColor.CGColor); 
  111.     //椭圆 
  112.     CGRect aRect= CGRectMake(80, 80, 160, 100); 
  113.     CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0); 
  114.     CGContextSetLineWidth(context, 3.0); 
  115.       CGContextSetFillColorWithColor(context, aColor.CGColor); 
  116.        CGContextAddRect(context, rect); //矩形 
  117.     CGContextAddEllipseInRect(context, aRect); //椭圆 
  118.     CGContextDrawPath(context, kCGPathStroke); 
  119.      */  
  120.    
  121.        
  122.        
  123.     /*  NO.11 
  124.      画一个实心的圆 
  125.    
  126.      CGContextFillEllipseInRect(context, CGRectMake(95, 95, 100.0, 100)); 
  127.     */  
  128.        
  129.        
  130.        
  131.     /*NO.12 
  132.      画一个菱形 
  133.     CGContextSetLineWidth(context, 2.0); 
  134.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  135.     CGContextMoveToPoint(context, 100, 100); 
  136.     CGContextAddLineToPoint(context, 150, 150); 
  137.     CGContextAddLineToPoint(context, 100, 200); 
  138.     CGContextAddLineToPoint(context, 50, 150); 
  139.     CGContextAddLineToPoint(context, 100, 100); 
  140.     CGContextStrokePath(context); 
  141.      */  
  142.    
  143.     /*NO.13 画矩形 
  144.     CGContextSetLineWidth(context, 2.0); 
  145.   
  146.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  147.   
  148.     CGRect rectangle = CGRectMake(60,170,200,80); 
  149.   
  150.     CGContextAddRect(context, rectangle); 
  151.       
  152.     CGContextStrokePath(context); 
  153.      */  
  154.        
  155.       
  156.     /*椭圆 
  157.     CGContextSetLineWidth(context, 2.0); 
  158.   
  159.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  160.   
  161.     CGRect rectangle = CGRectMake(60,170,200,80); 
  162.   
  163.     CGContextAddEllipseInRect(context, rectangle); 
  164.       
  165.     CGContextStrokePath(context); 
  166.      */  
  167.        
  168.     /*用红色填充了一段路径: 
  169.       
  170.     CGContextMoveToPoint(context, 100, 100); 
  171.     CGContextAddLineToPoint(context, 150, 150); 
  172.     CGContextAddLineToPoint(context, 100, 200); 
  173.     CGContextAddLineToPoint(context, 50, 150); 
  174.     CGContextAddLineToPoint(context, 100, 100); 
  175.     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
  176.     CGContextFillPath(context); 
  177.     */  
  178.        
  179.     /*填充一个蓝色边的红色矩形 
  180.     CGContextSetLineWidth(context, 2.0); 
  181.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  182.     CGRect rectangle = CGRectMake(60,170,200,80); 
  183.     CGContextAddRect(context, rectangle); 
  184.     CGContextStrokePath(context); 
  185.     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
  186.     CGContextFillRect(context, rectangle); 
  187.     */  
  188.        
  189.     /*画弧 
  190.      //弧线的是通过指定两个切点,还有角度,调用CGContextAddArcToPoint()绘制 
  191.     CGContextSetLineWidth(context, 2.0); 
  192.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  193.     CGContextMoveToPoint(context, 100, 100); 
  194.     CGContextAddArcToPoint(context, 100,200, 300,200, 100); 
  195.     CGContextStrokePath(context); 
  196.     */  
  197.       
  198.        
  199.     /* 
  200.     绘制贝兹曲线 
  201.     //贝兹曲线是通过移动一个起始点,然后通过两个控制点,还有一个中止点,调用CGContextAddCurveToPoint() 函数绘制 
  202.     CGContextSetLineWidth(context, 2.0); 
  203.   
  204.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  205.   
  206.     CGContextMoveToPoint(context, 10, 10); 
  207.   
  208.     CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400); 
  209.       
  210.     CGContextStrokePath(context); 
  211.      */  
  212.        
  213.     /*绘制二次贝兹曲线 
  214.       
  215.       CGContextSetLineWidth(context, 2.0); 
  216.   
  217.       CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  218.   
  219.       CGContextMoveToPoint(context, 10, 200); 
  220.   
  221.       CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200); 
  222.       
  223.       CGContextStrokePath(context); 
  224.      */  
  225.        
  226.     /*绘制虚线 
  227.     CGContextSetLineWidth(context, 5.0); 
  228.   
  229.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  230.   
  231.     CGFloat dashArray[] = {2,6,4,2}; 
  232.   
  233.     CGContextSetLineDash(context, 3, dashArray, 4);//跳过3个再画虚线,所以刚开始有6-(3-2)=5个虚点 
  234.       
  235.     CGContextMoveToPoint(context, 10, 200); 
  236.       
  237.     CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200); 
  238.       
  239.     CGContextStrokePath(context); 
  240.     */  
  241. /*绘制图片 
  242.     NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
  243.     UIImage* myImageObj = [[UIImage alloc] initWithContentsOfFile:imagePath]; 
  244.     //[myImageObj drawAtPoint:CGPointMake(0, 0)]; 
  245.     [myImageObj drawInRect:CGRectMake(0, 0, 320, 480)]; 
  246.   
  247.     NSString *s = @"我的小狗"; 
  248.   
  249.     [s drawAtPoint:CGPointMake(100, 0) withFont:[UIFont systemFontOfSize:34.0]]; 
  250. */  
  251.        
  252.   /* 
  253.     NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
  254.     UIImage *img = [UIImage imageWithContentsOfFile:path]; 
  255.     CGImageRef image = img.CGImage; 
  256.     CGContextSaveGState(context); 
  257.     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 
  258.     CGContextDrawImage(context, touchRect, image); 
  259.     CGContextRestoreGState(context); 
  260.    */  
  261.      
  262.        
  263.     /*NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
  264.     UIImage *img = [UIImage imageWithContentsOfFile:path]; 
  265.     CGImageRef image = img.CGImage; 
  266.     CGContextSaveGState(context); 
  267.   
  268.     CGContextRotateCTM(context, M_PI); 
  269.     CGContextTranslateCTM(context, -img.size.width, -img.size.height); 
  270.   
  271.     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 
  272.     CGContextDrawImage(context, touchRect, image); 
  273.     CGContextRestoreGState(context);*/  
  274.    
  275. /* 
  276.     NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
  277.     UIImage *img = [UIImage imageWithContentsOfFile:path]; 
  278.     CGImageRef image = img.CGImage; 
  279.       
  280.     CGContextSaveGState(context); 
  281.   
  282.     CGAffineTransform myAffine = CGAffineTransformMakeRotation(M_PI); 
  283.     myAffine = CGAffineTransformTranslate(myAffine, -img.size.width, -img.size.height); 
  284.     CGContextConcatCTM(context, myAffine); 
  285.   
  286.     CGContextRotateCTM(context, M_PI); 
  287.     CGContextTranslateCTM(context, -img.size.width, -img.size.height); 
  288.   
  289.     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 
  290.     CGContextDrawImage(context, touchRect, image); 
  291.     CGContextRestoreGState(context); 
  292. */  
  293. }  
### 光流法C++源代码解析与应用 #### 光流法原理 光流法是一种在计算机视觉领域中用于追踪视频序列中运动物体的方法。它基于亮度不变性假设,即场景中的点在时间上保持相同的灰度值,从而通过分析连续帧之间的像素变化来估计运动方向和速度。在数学上,光流场可以表示为像素位置和时间的一阶导数,即Ex、Ey(空间梯度)和Et(时间梯度),它们共同构成光流方程的基础。 #### C++实现细节 在给定的C++源代码片段中,`calculate`函数负责计算光流场。该函数接收一个图像缓冲区`buf`作为输入,并初始化了几个关键变量:`Ex`、`Ey`和`Et`分别代表沿x轴、y轴和时间轴的像素强度变化;`gray1`和`gray2`用于存储当前帧和前一帧的平均灰度值;`u`则表示计算出的光流矢量大小。 #### 图像处理流程 1. **初始化和预处理**:`memset`函数被用来清零`opticalflow`数组,它将保存计算出的光流数据。同时,`output`数组被填充为白色,这通常用于可视化结果。 2. **灰度计算**:对每一像素点进行处理,计算其灰度值。这里采用的是RGB通道平均值的计算方法,将每个像素的R、G、B值相加后除以3,得到一个近似灰度值。此步骤确保了计算过程的鲁棒性和效率。 3. **光流向量计算**:通过比较当前帧和前一帧的灰度值,计算出每个像素点的Ex、Ey和Et值。这里值得注意的是,光流向量的大小`u`是通过`Et`除以`sqrt(Ex^2 + Ey^2)`得到的,再乘以10进行量化处理,以减少计算复杂度。 4. **结果存储与阈值处理**:计算出的光流值被存储在`opticalflow`数组中。如果`u`的绝对值超过10,则认为该点存在显著运动,因此在`output`数组中将对应位置标记为黑色,形成运动区域的可视化效果。 5. **状态更新**:通过`memcpy`函数将当前帧复制到`prevframe`中,为下一次迭代做准备。 #### 扩展应用:Lukas-Kanade算法 除了上述基础的光流计算外,代码还提到了Lukas-Kanade算法的应用。这是一种更高级的光流计算方法,能够提供更精确的运动估计。在`ImgOpticalFlow`函数中,通过调用`cvCalcOpticalFlowLK`函数实现了这一算法,该函数接受前一帧和当前帧的灰度图,以及窗口大小等参数,返回像素级别的光流场信息。 在实际应用中,光流法常用于目标跟踪、运动检测、视频压缩等领域。通过深入理解和优化光流算法,可以进一步提升视频分析的准确性和实时性能。 光流法及其C++实现是计算机视觉领域的一个重要组成部分,通过对连续帧间像素变化的精细分析,能够有效捕捉和理解动态场景中的运动信息
微信小程序作为腾讯推出的一种轻型应用形式,因其便捷性与高效性,已广泛应用于日常生活中。以下为该平台的主要特性及配套资源说明: 特性方面: 操作便捷,即开即用:用户通过微信内搜索或扫描二维码即可直接使用,无需额外下载安装,减少了对手机存储空间的占用,也简化了使用流程。 多端兼容,统一开发:该平台支持在多种操作系统与设备上运行,开发者无需针对不同平台进行重复适配,可在一个统一的环境中完成开发工作。 功能丰富,接口完善:平台提供了多样化的API接口,便于开发者实现如支付功能、用户身份验证及消息通知等多样化需求。 社交整合,传播高效:小程序深度嵌入微信生态,能有效利用社交关系链,促进用户之间的互动与传播。 开发成本低,周期短:相比传统应用程序,小程序的开发投入更少,开发周期更短,有助于企业快速实现产品上线。 资源内容: “微信小程序-项目源码-原生开发框架-含效果截图示例”这一资料包,提供了完整的项目源码,并基于原生开发方式构建,确保了代码的稳定性与可维护性。内容涵盖项目结构、页面设计、功能模块等关键部分,配有详细说明与注释,便于使用者迅速理解并掌握开发方法。此外,还附有多个实际运行效果的截图,帮助用户直观了解功能实现情况,评估其在实际应用中的表现与价值。该资源适用于前端开发人员、技术爱好者及希望拓展业务的机构,具有较高的参考与使用价值。欢迎查阅,助力小程序开发实践。资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值