swift
class func drawRectInImage(qrFeature: CIQRCodeFeature, image: UIImage) -> UIImage
{
// 1. 获取图片大小
let size = image.size
// 2. 开启绘图上下文
UIGraphicsBeginImageContext(size)
// 3. 绘制图片到上下文中
image.drawInRect(CGRectMake(0, 0, size.width, size.height))
// 4. 反转坐标系(因为是别的二维码坐标是相对于图片的坐标, 坐标系是以竖屏左下角为(0,0)点,所以需要上下翻转坐标系(简单),或者转换坐标点)
// 相对于原始图片, 坐标
// 左下角是0, 0
// 所以, 我们需要翻转坐标系(采用这种方案比较简单), 或者转换坐标点
// 注意: 我们要求的是绘制的边框翻转, 而不是图片+边框
// 所以, 应该在绘制图片之后, 再翻转坐标系
let context = UIGraphicsGetCurrentContext()
CGContextScaleCTM(context, 1.0, -1.0); // 按Y轴, 缩放-1
CGContextTranslateCTM(context, 0, -size.height); // 需要往下平移一个图片高度
// 5. 绘制边框
// 5.1 拼接贝塞尔路径
let path: UIBezierPath = UIBezierPath(rect: qrFeature.bounds) // 根据二维码特征大小拼接路径
// 5.2 设置路径属性
UIColor.redColor().set()
path.lineWidth = 6
path.stroke()
// 6. 从上下文中获取绘制后的图片
let resultImage = UIGraphicsGetImageFromCurrentImageContext()
// 7. 关闭上下文
UIGraphicsEndImageContext()
// 返回图片
return resultImage
}