设置某些角为圆角, 使用UIBezierPath< class="hljs objectivec"> let layer = CAShapeLayer() layer.path = UIBezierPath(roundedRect: testView.bounds, byRoundingCorners: [UIRectCorner.TopLeft, .TopRight], cornerRadii: CGSize(width: 20, height: 20)).CGPath testView.layer.mask = layer > 设置view的四个角都为圆角, 最简单的方法, 使用当然最简单的某些情况下肯定就是不适合的, 因为这个方法会触发离屏渲染, 如果界面中有大量的圆角view的处理, 会出现掉帧的"现象" < class="hljs bash"> testView.layer.cornerRadius = 20.0 testView.layer.masksToBounds = true >缩放图片 3.1 < class="hljs objectivec">func resizeImage(image: UIImage, toWidth width: CGFloat, withHeight height: CGFloat) -> UIImage { let size = CGSize(width: width, height: height) UIGraphicsBeginImageContextWithOptions(size, false, 0) let context = UIGraphicsGetCurrentContext() CGContextTranslateCTM(context, 0.0, height) CGContextScaleCTM(context, 1.0, -1.0) CGContextSetBlendMode(context, .Copy) CGContextDrawImage(context, CGRect(origin: CGPointZero, size: size), image.CGImage) let retrivedImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return retrivedImage} >3.2 < class="hljs objectivec">func resizeImage(image: UIImage, toWidth width: CGFloat, withHeight height: CGFloat) -> UIImage { let size = CGSize(width: width, height: height) UIGraphicsBeginImageContextWithOptions(size, false, 0) image.drawInRect(CGRect(origin: CGPointZero, size: size)) let retrivedImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return retrivedImage} >
截屏功能 4.1 < class="hljs objectivec">func snapView(targetView: UIView) -> UIImage { UIGraphicsBeginImageContextWithOptions(targetView.bounds.size, false, 0) targetView.drawViewHierarchyInRect(targetView.bounds, afterScreenUpdates: false) let snapdImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return snapdImage} >4.2 < class="hljs objectivec"> let snapShotView = view.resizableSnapshotViewFromRect(view.bounds, afterScreenUpdates: false, withCapInsets: UIEdgeInsetsZero) >- 4.3
< class="hljs objectivec"> func snapView(targetView: UIView) -> UIImage { UIGraphicsBeginImageContextWithOptions(targetView.bounds.size, false, 0.0) targetView.layer.renderInContext(UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } >剪裁图片的指定部分 5.1 < class="hljs objectivec">func cutImage(image: UIImage, cutRect: CGRect) -> UIImage { UIGraphicsBeginImageContextWithOptions(cutRect.size, false, 0.0) let context = UIGraphicsGetCurrentContext() CGContextTranslateCTM(context, 0.0, cutRect.size.height) CGContextScaleCTM(context, 1.0, -1.0) CGContextSetBlendMode(context, .Copy) CGContextDrawImage(context, cutRect, image.CGImage) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image} >5.2 < class="hljs objectivec">func cutImage(image: UIImage, cutRect: CGRect) -> UIImage { UIGraphicsBeginImageContextWithOptions(cutRect.size, false, 0.0) let context = UIGraphicsGetCurrentContext() image.drawInRect(cutRect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image} >
将颜色绘制为图片 6.1 < class="hljs objectivec">func imageFromColor(color: UIColor, size: CGSize) -> UIImage { UIGraphicsBeginImageContextWithOptions(size, false, 0.0) let rect = CGRect(origin: CGPointZero, size: size) color.setFill() UIRectFill(rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image} >6.2 < class="hljs objectivec">func imageFromColor(color: UIColor, size: CGSize) -> UIImage { UIGraphicsBeginImageContextWithOptions(size, false, 0.0) let rect = CGRect(origin: CGPointZero, size: size) let context = UIGraphicsGetCurrentContext() CGContextSetFillColorWithColor(context, color.CGColor) CGContextFillRect(context, rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image} >
如果你觉得平时很少接触过Quartzs 2D来绘图, 可能看上面的代码不是很明白, 如果想要更清楚, 建议阅读相关的绘图教程, 同时下一篇可能会简单分享一下使用Quartzs 2D来绘图的相关知识, 欢迎关注
文/ZeroJ(简书作者) 原文链接:http://www.jianshu.com/p/524454440bea 著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
|