【问题标题】:How to save user input drawing over image to photo gallery如何将用户输入的图像保存到照片库
【发布时间】:2019-07-24 03:12:00
【问题描述】:

我的代码成功地将图像保存到照片库,但是我在该图像上绘制的内容并没有保存在它上面。所以我的代码需要保存图像和用户输入的绘图。现在它只是保存图像。

 var nextPage = UIButton()
    var clear = UIButton()
    var submitToDB = UIButton()





    var myLayer = CALayer()
    var path = UIBezierPath()
    var startPoint = CGPoint()
    var touchPoint = CGPoint()



    var playName = UITextField()





    var drawPlace = UIImageView()//The imageview






    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        if let point = touch?.location(in: drawPlace){
            startPoint = point

        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        if let point = touch?.location(in: drawPlace){
            touchPoint = point
        }

        path.move(to: startPoint)
        path.addLine(to: touchPoint)
        startPoint = touchPoint
        draw()
    }
    func draw() {
        submitToDB.layer.cornerRadius = 0
        drawPlace.clipsToBounds = true
        drawPlace.isMultipleTouchEnabled = false
        let strokeLayer = CAShapeLayer()
        strokeLayer.fillColor = nil
        strokeLayer.lineWidth = 5
        strokeLayer.strokeColor = UIColor.blue.cgColor
        strokeLayer.path = path.cgPath
        drawPlace.layer.insertSublayer(strokeLayer, below: myLayer)
        drawPlace.setNeedsDisplay()


        let star:UIImage = UIImage(named: "bb.png")!
        let newSize = CGSize(width: star.size.width, height: star.size.height  )

        UIGraphicsBeginImageContextWithOptions(newSize, false, star.scale)



        star.draw(in: CGRect(x: newSize.width/10.6,
                             y: newSize.height/8,
                             width: newSize.width/1.2,
                             height: newSize.height/1.2),
                  blendMode:CGBlendMode.normal, alpha:1)



        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()


        drawPlace.image = newImage





    }
    func save() {




        guard let selectedImage = drawPlace.image else {
            print("Image not found!")
            return
        }
        UIImageWriteToSavedPhotosAlbum(selectedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
    }

我想我需要以某种方式将图层保存在正在保存的图像上,我只是不知道该怎么做。

【问题讨论】:

    标签: swift image calayer uibezierpath photo-gallery


    【解决方案1】:

    您需要从UIImageView 进行捕获,所以试试这个:

    将扩展名添加到UIView 以进行捕获:

    extension UIView {
        func screenshot() -> UIImage {
            return UIGraphicsImageRenderer(size: bounds.size).image { _ in
                drawHierarchy(in: CGRect(origin: .zero, size: bounds.size), afterScreenUpdates: true)
            }
        }
    }
    

    之后,将您的 save() 更改为:

    func save() {
        let image = drawPlace.screenshot()
        UIImageWriteToSavedPhotosAlbum(image, self,  #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-20
      • 2018-10-03
      • 2023-03-26
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      相关资源
      最近更新 更多