簡體   English   中英

在UIImage上繪制另一個圖像-

[英]Draw another image on a UIImage -

我正在嘗試實現在UIImageView的特定點繪制圖像的功能。

1.)因此,用戶制作了一張照片2.)在“ Aspect Fit”中的UIImageView中將照片顯示給用戶。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
}}

資源

    @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); 但是,沒有任何工作正常。

我想念什么?

謝謝! 莎拉

您肯定在正確的軌道上。 當以“寬高比”內容模式顯示圖像時,手頭有一個實用的實用程序將圖像視圖邊界中的點轉換為圖像邊界中的點將很有用:

extension UIImageView {
    func convertPointToImageCoordinates(_ p:CGPoint) -> CGPoint {
        switch self.contentMode {
        case .scaleAspectFit:
            let r = AVMakeRect(
                 aspectRatio: self.image!.size, insideRect: self.bounds)
            let scale = self.image!.size.width / r.width
            var p2 = CGPoint(x: p.x - r.minX, y: p.y - r.minY)
            p2 = CGPoint(x: p2.x * scale, y: p2.y * scale)
            return p2
        default:
            fatalError("not written")
        }
    }
}

現在,您可以獲取相對於原始圖像的攻絲坐標,因此可以決定相對於原始圖像放置檢查標記圖像的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM