iPhone绘图

news/2024/7/8 5:29:22 标签: iphone, 图形, quartz, uiview, transformation, null

iphone
主要通过下面的几个技术来绘图
OpenGL, Quartz, UIKit, or Core Animation

UIKit 是非线程安全的,所以最好把所有的绘图都放在主线程上执行
不管使用的哪个技术来绘图,所有的绘图都是在 UIView object 中进行, view决定绘图在那里进行

绘画周期
当一个view需要更新某一部分内容的时候,view会请求 drawRect: 方法
在view第一次请求drawRect方法的时候,传递的rectangle 参数一般是view的整个rectangle ,后续更新的时候,传递的一般是
需要更新的那部分rectangle 

在几种情况下,view会重新绘图
1。移动或者移除另外一个view
2。设置view的hidden 属性为NO, view重新出现
3。滚动view,当滚出或者滚进来的时候
4。明确的请求setNeedsDisplay和setNeedsDisplayInRect:方法

当请求了一个 drawRect:方法,view会标志自己已经被更新了,然后等待下一个更新请求的到达

坐标系统
current transformation matrix (CTM)
默认坐标原点是左上角
如果需要改变坐标系统
有两种方法
1。 CGContext Reference :http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html#//apple_ref/doc/uid/TP30000950
2。 CGAffineTransform :http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html#//apple_ref/c/tdef/CGAffineTransform

图形上下文(Graphics Contexts)
当请求drawRect:方法 ,view object会自动配置图形环境,作为环境的一部分,uiview会创建一个图形上下文(a CGContextRef opaque type)
图形上下文定义基本图形属性,如颜色,剪切区域,线的宽度和样式信息,字体信息,合成选项,等等。
也可以自己创建图形上下文用 CGBitmapContextCreate 或者 CGPDFContextCreate 函数
需要注意的是,自己创建的图形上下文的原点是在左下角
CGContextSetRGBStrokeColor and CGContextSetRGBFillColor两个函数设置当前的笔锋色和填充色.

iphone支持的图形格式
.png
.tiff, .tif
.jpeg, .jpg
.gif
.bmp, .BMPf
.ico
.cur
.xbm


绘画技巧
1。部分更新: 假如在 drawRect: 中,更新rectangle 中的部分
2。如果一个view中没有透明部分,那么把 opaque 属性设置为 YES,这样会省很多的cpu
3。如果一张png图片没有任何透明的部分,那么久删除alpha通道,这样渲染的时候会省很多功夫
4。滚动的时候重用table cells和views
5。正常情况下,在view请求 drawRect: 之前都会清除current context buffer,来更新相同区域.如果在滚动的时候,反复的清除,
很浪费时间,这样的话就把view的clearsContextBeforeDrawing 设置成NO.
6。在绘图的时候,尽量少的图形状态改变.因为改变绘图状态需要window的server

提高图片质量
1。首选png图片格式
2。使用图片的时候,尽量的不要去改变大小,假如需要使用这个图片在很多地方,那么尽量使用和他们比较接近的图片大小的图片

Quartz 是Core Graphics的心脏, 主要提供以下东西
Graphics contexts
Paths
Images and bitmaps
Transparency layers
Colors, pattern colors, and color spaces
Gradients and shadings
Fonts
PDF content
更 多的详细内容在:http://developer.apple.com/iphone/library/documentation /CoreGraphics/Reference/CoreGraphics_Framework/index.html#//apple_ref/doc/uid/TP40007127

UIKit 是在Quartz的基本功能上的封装.他主要提供以下类
1。UIImage
2。UIColor
3。UIFont
4。UIScreen
5。生成png或者jpeg,用UIImage表现出来的函数
6。画矩形,和剪裁绘图区域的函数
7。改变和获取当前的图形上下文
更 多的内容在:http://developer.apple.com/iphone/library/documentation/UIKit /Reference/UIKit_Framework/index.html#//apple_ref/doc/uid/TP40006955

配置图形上下文
在drawRect:中,view已经自动的为我们创建了图形上下文,我们可以通过函数UIGraphicsGetCurrentContext 获取.


 
图形上下文使用堆栈来保存图像状态,CGContextSaveGState函数保存当前图像状态
CGContextRestoreGState函数来回到前面的版本

图片的绘画和创建
下面的几个场景,最好使用下面的方法
1>当view中只有一张图片,那么使用UIImageView 来加载图片
2>用代码创建一张图片
两种方法,
1, 先用UIGraphicsBeginImageContext 创建一个基于图片的图形上下文
画好图形后,用UIGraphicsGetImageFromCurrentImageContext 函数,生成图片
画完,最后用UIGraphicsEndImageContext 关闭图形上下文
2。用CGBitmapContextCreate 创建图形上下文,
在上面画图片,用CGBitmapContextCreateImage 创建CGImageRef 
最后用CGImageRef来创建 UIImage 
3>把一张图片保存为jpg或者png
加载一张图片,然后用UIImageJPEGRepresentation 或者UIImagePNGRepresentation 函数获取加载的图片的NSData ,然后用
NSData生成png或者jpg 

创建和绘制路径
一个路径是一个二维几何场景,
UIKit 中包含 UIRectFrame UIRectFill 这 两个方法来创建简单的路径,比如矩形.
Core Graphics中还包含了椭圆,等等.
CGContextBeginPath 来创建一个基于路径的图形上下文,然后开始创建路径
完事后,生成 CGPathRef 或者 CGMutablePathRef 
最后用CGContextStrokePath 和CGContextFillPath 填充颜色.

参 考:http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/GraphicsandDrawing/GraphicsandDrawing.html#//apple_ref/doc/uid/TP40007072-CH10-SW7





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

1、绘图总结:

绘图前设置:

 
  1. CGContextSetRGBFillColor/CGContextSetFillColorWithColor  //填充色   
  2. CGContextSetRGBStrokeColor/CGContextSetStrokeColorWithColor //笔颜色   
  3. CGContextSetLineWidth   //线宽度  

绘图后设置:

注:  画完图后,必须 先用CGContextStrokePath来描线,即形状,后用CGContextFillPath来填充形状内的颜色.

2.常见图形绘制:

 
  1. CGContextFillRect/CGContextFillRects   
  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.  
  3. UIGraphicsBeginImageContext(yourView.frame.size);   
  4. [[yourView layer] renderInContext:UIGraphicsGetCurrentContext()];   
  5. UIImage*screenshot =UIGraphicsGetImageFromCurrentImageContext();   
  6. UIGraphicsEndImageContext();   
  7. from:http://www.cppblog.com/zhangyuntaoshe/articles/123066.html  

合并两张bit图到一张image的方法

 
  1. To graphically merge two images into a new image, 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. [image drawInRect:imageRect];   
  16. [image2 drawInRect:image2Rect];   
  17. UIGraphicsPopContext();   
  18. CGImageRef imageRef = CGBitmapContextCreateImage(context);   
  19. if (imageRef != NULL) {   
  20. result = [UIImageimageWithCGImage:imageRef];   
  21. CGImageRelease(imageRef);   
  22. }   
  23. CGContextRelease(context);   
  24. }   
  25. free(data);   
  26. }   
  27. return result;  

关键方法: 

 
  1. CGContextRef context = CGBitmapContextCreate();   
  2. CGContextTranslateCTM();   
  3. CGContextScaleCTM();   
  4. CGImageRef imageRef = CGBitmapContextCreateImage(context);   
  5. CGImageRelease(imageRef); 

小结:iPhone图形开发绘图教程的内容介绍完了,希望本文对你有所帮助!


iphone 绘图 

http://blog.csdn.net/yuhuangc/article/details/7066699

iPhone 绘图实现http://hi.baidu.com/492437598/item/f6bea6154a85c00f8fbde412




http://www.niftyadmin.cn/n/591290.html

相关文章

单体到微服务是一个演化过程,别在一开始就过度设计

大多数应用程序&#xff08;可能是其中的90&#xff05;&#xff09;采用了单体架构。为了避免过度工程化&#xff0c;我们应该从一个简单的架构开始&#xff0c;并根据需求进行演变。在Reactive Summit 2018大会上&#xff0c;Randy Shoup在演讲中分享了他与小公司一起&#x…

android重新签名APK文件

1. 生成Android APK包签名证书 1). 在doc中切换到jdk的bin目录 cd C:\Program Files\Java\jdk1.6.0_18\bin 2). 运行下面的命令 keytool –genkey –alias android123.keystore –keyalg RSA –validity 20000 –keystore android123.keystore /*解释&#xff1a;…

HBuildX打包Android .9图片 Output: error: too many padding sections on right border.

HBuildX打包Android .9图片 Output: error: too many padding sections on right border. HBuildX错误截图如下&#xff1a; 错误关键信息 Output: error: too many padding sections on right border.可查看附件文件中错误日志:(错误日志也附带在文章末尾) java.util.concur…

Vue Uncaught TypeError: cannot assign to read only property

Uncaught TypeError: /cannot assign to read only property ‘exports’ of object # 前言 公司运维小伙伴&#xff0c;拉了个开源的xops运营工具&#xff0c;前端代码是Vue的工程&#xff0c;项目npm install(cnpm install )、npm run dev 后&#xff0c;页面浏览器打开&…

如何通过学校系统漏洞注册到 @edu.cn 邮箱账号? ...

此文章仅针对我自己学校的系统进行分析&#xff0c;并不代表所有学校的系统都是如此。我们学校比较“抠”&#xff0c;可能是为了节省学校的带宽资源然后禁止学生注册教育邮箱账号。不过像一部电影所说的那样“没有绝对安全的系统”&#xff0c;有时候如果多动一下脑子并不需要…

git使用-烂笔头(1)git撤销 git reset --hard ********

**git使用-烂笔头&#xff08;1&#xff09;git reset --hard ********** 前言 为什么需要使用git撤销这个操作。使用SourceTree git图形化界面管理工具。 通知在贮藏之后&#xff0c;并没有拉取 最新代码&#xff0c;实际上&#xff0c;主分支上已经有最新一次代码提交。正确…

NumPy 1.16.0 是最后一个支持 Python 2.7 的版本

开发四年只会写业务代码&#xff0c;分布式高并发都不会还做程序员&#xff1f; NumPy 项目在 2017 年已经宣布将停止支持 Python 2。现在它发布了最后一个支持 Python 2.7 的版本 1.16.0&#xff0c;它将一直支持到 2020 年。开源科学计算包 NumPy 系统是 Python 的一种开源…

objective-c类型转换

NSInteger转化NSString类型&#xff1a;[NSString stringWithFormat: "%d", NSInteger];NSString转化 NSInteger类型&#xff1a;NSInteger [NSString intValue];NSNumber转化 NSStringNSNumberFormatter* numberFormatter [[NSNumberFormatter alloc] init];NSStr…