如何将 2 个图像保存在彼此之上 (swift3)

how to save 2 images on top of each other (swift3)

我的代码现在只保存 1 张图像。我想在第一张图片上方的左上角保存一张较小的图片。最重要的是让它一起保存在照片库中。 2 张图像彼此重叠。

import UIKit

class ViewController: UIViewController {


let imageView = UIImageView(frame: CGRect(x: 50, y: 50, width: 300, height: 300))
let imageView2 = UIImageView(frame: CGRect(x:200, y: 50, width: 100, height: 100))



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let image =  UIImage(named: "wall")!
    imageView.image = image


    let image2 = UIImage(named: "pic")!
    imageView2.image = image2

    imageView.addSubview(imageView2)


    imageView2.image = image2



    imageView.addSubview(imageView2)

    let topImage = UIImage(named: "wall")
    let bottomImage = UIImage(named: "pic")

    let size = CGSize(width: topImage!.size.width, height: topImage!.size.height + bottomImage!.size.height)
    UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
    topImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height))
    bottomImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height))

    //your new Image
    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
                   UIGraphicsEndImageContext()

}

@IBAction func press(_ sender: Any) {
     UIImageWriteToSavedPhotosAlbum(imageView.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
    if let error = error {
        // we got back an error!
        let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
    } else {
        let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
    }}}

pic

    let imageView2 = UIImageView(frame: CGRect(x:200, y: 50, width: 100, height:100))
    let image2 =  UIImage(named: "wallsmallImage")!
    imageView2.image = image2
    imageView.addSub(imageView2)

#Now merge both images
         let image =  UIImage(named: "wall")!
         imageView.image = image
        let image2 = UIImage(named: "pic")!
        imageView2.image = image2

       imageView.addSubview(imageView2)

        let topImage = UIImage(named: "wall")
        let bottomImage = UIImage(named: "pic")

        let size = CGSize(width: topImage!.size.width, height: topImage!.size.height + bottomImage!.size.height)
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
        topImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height))
         bottomImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height))

        //your new Image
        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

所有这些代码都进入了一个不同的函数,以将其添加到图像视图中。

     let bottomImage:UIImage = UIImage(named: "backdrop")!
        let topImage:UIImage = UIImage(named:"wall")!
        let topImage2:UIImage = UIImage(named:"wall")!
        let tpp:UIImage = letImageVIEW.image!
                  let right:UIImage = rightIMAGEVIEW.image!

        // Change here the new image size if you want
        let newSize = CGSize(width: bottomImage.size.width, height: bottomImage.size.height  )
        let newSize2 = CGSize(width: bottomImage.size.width, height: bottomImage.size.height)
        UIGraphicsBeginImageContextWithOptions(newSize, false, bottomImage.scale)
        //        bottomImage.draw(in: CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height))
              tpp.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
              right.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height: newSize2.height), blendMode:CGBlendMode.normal, alpha:0.2)

        topImage.draw(in: CGRect(x: 0,y: 0,width: newSize.width/2,height: newSize.height/2), blendMode:CGBlendMode.normal, alpha:1.0)
        topImage2.draw(in: CGRect(x: 50,y: 0,width: newSize2.width/4,height: newSize2.height/4), blendMode:CGBlendMode.normal, alpha:1.0)





        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        finalImage.image = newImage