如何将 2 张图像重叠保存(swift3)

Posted

技术标签:

【中文标题】如何将 2 张图像重叠保存(swift3)【英文标题】:how to save 2 images on top of each other (swift3) 【发布时间】:2017-06-05 23:35:47 【问题描述】:

我的代码现在只保存 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

【问题讨论】:

【参考方案1】:
    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()

【讨论】:

我添加了您的代码,但仍然只保存了 1 张图像。我用你建议的代码编辑了我的问题。 我添加了您的代码,在我看来,所有这些都在我上面的问题中加载了函数。有一些错误。我添加了一张图片,以便您可以看到错误。 hii @SamBurns ,代码已编辑..请检查..您可以将新的合并图像保存到库中 @V12 我用你隐含的解决方案更新了我的代码。见上面的问题。不确定我是否将代码放在正确的位置。但是代码仍然只保存 1 张图像。 “墙”总是被保存,我看不到“图片”。因此,只有在保存照片时才能看到 1 张图片。 @MahipalSingh 我用您隐含的解决方案更新了我的代码。见上面的问题。不确定我是否将代码放在正确的位置。但是代码仍然只保存 1 张图像。 “墙”总是被保存,我看不到“图片”。因此,只有在保存照片时才能看到 1 张图片。【参考方案2】:

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

     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

【讨论】:

以上是关于如何将 2 张图像重叠保存(swift3)的主要内容,如果未能解决你的问题,请参考以下文章

opencv 如何计算两张图像重叠的区域并计算出重叠度?

如何使用Matlab将图像裁剪为重叠的块?

如何快速将几张图像作为一张图像保存到照片库?

将两张重叠的图片合二为一,iOS

拖放 2 张图像以将它们放置在背景上并将它们保存为 1 张组合图像?

单独保存模板匹配 OpenCV Python