在UIImage上绘制另一个图像 -

时间:2018-06-10 15:10:21

标签: ios swift uiimagepickercontroller uitapgesturerecognizer

我正在尝试实现一个在UIImageView的特定点绘制图像的功能。

1。)所以用户拍照 2.)照片在UIImageView中显示给用户" Aspect Fit" 3.)用户点击UIImageView将图像放在点击点 4.)显示组合图像

extension UIImage {
func image(byDrawingImage image: UIImage, inRect rect: CGRect) -> UIImage! {
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
    image.draw(in: rect)
    let result = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return result
}}

source

    @IBAction func imageTapped(_ sender: UITapGestureRecognizer) {


    let pointTapped = sender.location(in: imageDIsplay)

    imageDIsplay.image = originalImage?.image(byDrawingImage: #imageLiteral(resourceName: "checkmark.png"), inRect: CGRect(x: originalImage!.size.width / imageDIsplay.frame.width * pointTapped.x, y: originalImage!.size.height / imageDIsplay.frame.height * pointTapped.y, width: 100, height: 100))

}

我的问题:"勾选图片"永远不会在我点击的确切位置。

我尝试了很多而不是originalImage!.size.width / imageDIsplay.frame.width来获得更好的比率,包括CGRect AVMakeRectWithAspectRatioInsideRect(CGSize aspectRatio, CGRect boundingRect);。但没有一点正常。

我想念什么?

谢谢! 萨拉

1 个答案:

答案 0 :(得分:2)

你肯定是在正确的轨道上。当图像以“宽高比适合”内容模式显示时,有一个工作utility用于将图像视图边界中的点转换为图像边界中的点将是有用的:

comparse.add_argument(message, "vel", "int", 10, "this is your velocity attribute")

现在,您可以获取相对于原始图像的点击坐标,这样您就可以决定将复选标记图像放在哪里。