简单的画图

iOS上的图像和图形处理
UIColor.init(red: <#T##CGFloat#>, green: <#T##CGFloat#>, blue: <#T##CGFloat#>, alpha: <#T##CGFloat#>)
在这个函数中,所有的参数的范围都是0.0-1.0,所以有时候在用这个函数配置颜色是,当直接传入RGB值的时候,得不到想要的色泽,这个时候要将RGB的参数同时除以255换算成0.0-1.0之间才能显示效果
在绘图的时候可以调用UIcolor的实例方法set(),setFill(),setStroke()进行填充,在调用其cgColor方法的时候会回访cgColorRef,其是cgcolor的引用。UIcolor是cgcolor的封装,Cgcolor中的components,会返回一个数组即[R,G,B,a],所以可以通过这个方法获取颜色的值,以前做过一个项目,就是把导航栏和tableview的headerView的颜色设置的一样,怎么样都没有成功,所以.....我就放了两张图片上去,因为当时还不知道有这个方法
self.navigationController?.navigationBar.setBackgroundImage(UIImage.init(named: "navigationImage"), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)

  • self.navigationController?.navigationBar.shadowImage = UIImage()

 

 

 

 

绘图的时候可以在UIView的draw(_ rect: CGRect)函数中,在UIKit中有绘制文字和图片的方法,如下
let str:NSString = "绘制文字"
str.draw(at: CGPoint.init(x: 18, y: 20), withAttributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 40)])

str.draw(at: CGPoint.init(x: 18, y: 70), withAttributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 40),NSKernAttributeName:5,NSForegroundColorAttributeName:color])
let image = UIImage.init(named: "navigationImage")
image?.draw(at: CGPoint.init(x: 18, y: 180))
image?.draw(in: CGRect.init(x: 18, y: 250, width: 100, height: 100))
在绘制文字的时候,可以通过Attributes来设置大小,颜色等属性,以前的可以直接设置,现在废弃掉了,
绘制的时候也可以用iOS较低层的Core Graphics的api,下面是绘制线段的方法
let color = UIColor.red
color.setStroke()
let context = UIGraphicsGetCurrentContext()
context!.setLineWidth(5.0)
context?.move(to: CGPoint.init(x: 20, y: 20))
context?.addLine(to: CGPoint.init(x: 45, y: 370))
context?.strokePath()
在绘制的时候,路径是缓冲区,可以不会立刻绘制到图形上下文中,其会在适当的时候进行绘制,如果想要立刻绘制出来,需要手动调用setNeedsDisplay()
CGMutablePath是绘制路径的可以绘制各种图形和线条
let color = UIColor.red
color.setStroke()
let SCREEN = UIScreen.main.bounds
let path = CGMutablePath.init()
path.move(to: CGPoint.init(x: 0, y: 0))
path.addLine(to: CGPoint.init(x: SCREEN.width, y: SCREEN.height))
path.move(to: CGPoint.init(x: SCREEN.width, y: 0))
path.addLine(to: CGPoint.init(x: 0, y: SCREEN.height))
let context = UIGraphicsGetCurrentContext()
context!.setLineWidth(5.0)
context?.addPath(path)//把路径添加到上下文中准备绘制
context?.strokePath()//绘制路径

 

 

posted @ 2017-08-03 21:33  月亮和便士  阅读(341)  评论(0编辑  收藏  举报