class MenuController: UIVIEwController { overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // Do any additional setup after loading the vIEw,typically from a nib. self.vIEw.backgroundcolor = UIcolor.blackcolor() var logoFrame = CGRectMake(0,118,40) var imageVIEw = UIImageVIEw(frame: logoFrame) imageVIEw.image = UIImage(named:"logo") self.vIEw.addSubvIEw(imageVIEw) //need to draw a rectangle here }}这是一个创建一个包含透明背景和一个红色矩形的自定义UIImage的例子,线条通过对角线对齐。
class VIEwController: UIVIEwController { overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad(); let imageSize = CGSize(wIDth: 200,height: 200) let imageVIEw = UIImageVIEw(frame: CGRect(origin: CGPoint(x: 100,y: 100),size: imageSize)) self.vIEw.addSubvIEw(imageVIEw) let image = drawCustomImage(imageSize) imageVIEw.image = image }}func drawCustomImage(size: CGSize) -> UIImage { // Setup our context let bounds = CGRect(origin: CGPoint.zeroPoint,size: size) let opaque = false let scale: CGfloat = 0 UIGraphicsBeginImageContextWithOptions(size,opaque,scale) let context = UIGraphicsGetCurrentContext() // Setup complete,do drawing here CGContextSetstrokecolorWithcolor(context,UIcolor.redcolor().CGcolor) CGContextSetlinewidth(context,2.0) CGContextstrokeRect(context,bounds) CGContextBeginPath(context) CGContextMovetoPoint(context,CGRectGetMinX(bounds),CGRectGetMinY(bounds)) CGContextAddlinetoPoint(context,CGRectGetMaxX(bounds),CGRectGetMaxY(bounds)) CGContextMovetoPoint(context,CGRectGetMaxY(bounds)) CGContextstrokePath(context) // Drawing complete,retrIEve the finished image and cleanup let image = UIGraphicsGetimageFromCurrentimageContext() UIGraphicsEndImageContext() return image}总结
以上是内存溢出为你收集整理的如何使用CGContext在swift中以程序方式绘制矩形/线条全部内容,希望文章能够帮你解决如何使用CGContext在swift中以程序方式绘制矩形/线条所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)