swift - 如何将用户输入的图像图像保存到照片库

标签 swift image calayer uibezierpath photo-gallery

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

 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)
    }

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

最佳答案

您需要从 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)
}

关于swift - 如何将用户输入的图像图像保存到照片库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57174698/

相关文章:

ios - 如何使用回调在 Swift 中进行异步调用,以便主 UI 不会挂起?

ios - setValue(value, forKey) 以某种方式改变了 Realm 中的整个数组

上传时调整图像大小的 php 会在我不希望图像旋转时旋转图像

ios - 将字典追加到 Swift 数组

arrays - Swift 数组不会填充 JSON 数据

image - 没有这样的图像 : docker. io/openshift/origin-node-problem-detector :v3. 11.0

android - 将位图保存为 jpeg 文件

ios - 带有切出段和阴影的 UIView

swift - 看不到CALayer

iphone - 导致此错误的原因是什么? "CALayer position contains NaN: [240 nan]"