Ios 在图像上快速绘制图像资源

Ios 在图像上快速绘制图像资源,ios,swift,uigraphicscontext,Ios,Swift,Uigraphicscontext,我试图在Swift 4中的图像上绘制一个名为GreyCircle(小灰圈,25x25)的图像资源,但我似乎无法正确绘制。顺便说一下,这是一个点击手势功能,也就是说,我试图画一个灰色的圆圈,用户点击一张照片显示图像保存当前显示的图像 代码: greyCircle.draw行不正确,但这是我最近的一次尝试。我确信我遗漏了一些令人毛骨悚然的东西。您想将接触点转换为图像视图的边界。然后在绘制圆时,使用圆的大小,而不是图像的大小 let touchPoint = gestureRecognizer.loc

我试图在Swift 4中的图像上绘制一个名为GreyCircle(小灰圈,25x25)的图像资源,但我似乎无法正确绘制。顺便说一下,这是一个点击手势功能,也就是说,我试图画一个灰色的圆圈,用户点击一张照片<代码>显示图像保存当前显示的图像

代码:


greyCircle.draw
行不正确,但这是我最近的一次尝试。我确信我遗漏了一些令人毛骨悚然的东西。

您想将接触点转换为图像视图的边界。然后在绘制圆时,使用圆的大小,而不是图像的大小

let touchPoint = gestureRecognizer.location(in: displayedImage)
let greyCircle = UIImage(named: "GreyCircle")   

UIGraphicsBeginImageContextWithOptions(displayedImage.size, false, 0.0)
displayedImage.draw(in: CGRect(x:0, y:0, width:displayedImage.size.width, height: displayedImage.size.height))
greyCircle.draw(in: CGRect(x:touchPoint.x, y:touchPoint.y, width: greyCircle.size.width, height: greyCircle.size.height))
let newImag = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()
displayedImage = newImag

为什么要用displayImage的大小绘制灰色圆圈?当我尝试执行“let touchPoint=gestureRecognitor.location(in:DisplayeImage)”时,我得到了红色感叹号错误,无法转换“UIImage!”类型的值应该是参数类型“UIView?”。哦。我错误地认为
displayedImage
。你有图像视图吗?用你的图像视图替换那一行上的
displayedImage
。我不想澄清这一点。taggedImage是我现在尝试的,它是一个UIImageView IBOutlet,扩展到它所在的ViewController的边界。当用户访问此VC时,他们会看到包含整个屏幕的照片。当他们点击时,我希望在他们点击的地方绘制图像资源灰色圆圈(有效地标记图像)。所以我替换了taggedImage,在那里我显示了eImage,但它仍然不工作。非常感谢您的进一步见解。仅供参考,我成功地绘制了文本标签,但我无法让他绘制该死的灰色圆圈。灰色圆圈没有在图像上绘制。我知道点击正在发生,因为我正在将其记录到控制台。请确认
touchPoint
greyCircle
的值。使用调试器。看看发生了什么。
let touchPoint = gestureRecognizer.location(in: displayedImage)
let greyCircle = UIImage(named: "GreyCircle")   

UIGraphicsBeginImageContextWithOptions(displayedImage.size, false, 0.0)
displayedImage.draw(in: CGRect(x:0, y:0, width:displayedImage.size.width, height: displayedImage.size.height))
greyCircle.draw(in: CGRect(x:touchPoint.x, y:touchPoint.y, width: greyCircle.size.width, height: greyCircle.size.height))
let newImag = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()
displayedImage = newImag