将另一张图片上的图片保存到相册

时间:2019-07-11 04:41:22

标签: swift imageview save layer photo-gallery

我想将一个图像保存在另一个图像上。假设图片1和2。图片1在底部。图片2以50%的透明度排在最前面。我已经附上了下面的代码。我只希望用户将图像保存在图像视图上的当前图像上。图片1是aaa,图片2是bbb

var image1 = UIImageView()
var image2 = UIImage(named: "bbb.png")

override func viewDidLoad() {
    super.viewDidLoad()
    nextPage.addTarget(self, action: #selector(moveRight), for: .touchUpInside)
 image1.image = UIImage(named: "aaa.png")  
}

@objc func moveRight() {
    save()
}

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

//MARK: - Add image to Library
@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
    if let error = error {
        // we got back an error!
        showAlertWith(title: "Save error", message: error.localizedDescription)
    } else {
        showAlertWith(title: "Saved!", message: "Your image has been saved to your photos.")
    }
}

func showAlertWith(title: String, message: String){
    let ac = UIAlertController(title: title, message: message, preferredStyle: .alert)
    ac.addAction(UIAlertAction(title: "OK", style: .default))
    present(ac, animated: true)
}

0 个答案:

没有答案