【发布时间】:2016-05-21 21:13:28
【问题描述】:
我正在尝试使用 Core Graphics 在 UIImage 的中心放置一个矩形,大致如下所示:
这是我目前的代码:
func drawRectangleOnImage(image: UIImage) -> UIImage {
let imageSize = image.size
let scale: CGFloat = 0
UIGraphicsBeginImageContextWithOptions(imageSize, false, scale)
let context = UIGraphicsGetCurrentContext()
let rectangle = CGRect(x: 0, y: (imageSize.height/2) - 30, width: imageSize.width, height: 60)
CGContextSetFillColorWithColor(context, UIColor.blackColor().CGColor)
CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor)
CGContextSetLineWidth(context, 5)
CGContextAddRect(context, rectangle)
CGContextDrawPath(context, .Fill)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
我觉得好像它不是在我发送的图像之上绘制,它完全是在创建一个新图像。整个东西变成红色,矩形黑色。我想要黑色矩形,其余部分仍应与图像相同。
我做错了什么?
【问题讨论】:
-
我想将图像绘制到上下文中会是一个好的开始;)
标签: ios swift uiimage core-graphics