简体   繁体   English

如何在另一个uiimageview(swift3)之上保存uiimageview

[英]How to save a uiimageview on top of another uiimageview (swift3)

Right Now I have two image views. 现在我有两个图像视图。 All the button does is save the photo with the red box over the blue box and it will save image2 in the blue box and image1 in the red box. 所有按钮都将红色框保存在蓝色框上,并将图像2保存在蓝色框中,图像1保存在红色框中。 I would like red box to be saved over the blue box in just the small bottom right section of the blue box. 我想要将红色框保存在蓝色框的右下方的蓝色框中。 Just like a lower third you see on tv ]] 就像你在电视上看到的低三分之一]]

在此输入图像描述 import UIKit 导入UIKit

  class ViewController: UIViewController {


var image1 = UIImage(named: "f.jpg")
var image2 = UIImage(named: "l.jpg")
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.




}
func saveImage(image1:UIImage, image2:UIImage) -> UIImage {

    let size1 = image1.size
    let size2 = image2.size

    let origin = CGPoint(x: size1.width-size2.width, y: size1.height - size2.height)

    UIGraphicsBeginImageContextWithOptions(size2, false, 0.0)

    image1.draw(in: CGRect(origin: CGPoint.zero, size: size1))
    image2.draw(in: CGRect(origin: origin, size: size2))

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
}
@IBAction func d(_ sender: Any) {
    UIImageWriteToSavedPhotosAlbum(saveImage(image1:image1!, image2:image2!), 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)
    }

 }}

照片库中保存的照片

Try this 尝试这个

// returns a new image where image2 in the bottom right corner of image1

func saveImage(image1:UIImage, image2:UIImage) -> UIImage {

    let size1 = image1.size
    let size2 = image2.size

    let origin = CGPoint(x: size1.width-size2.width, y: size1.height - size2.height)

    UIGraphicsBeginImageContextWithOptions(size1, false, 0.0)

    image1.draw(in: CGRect(origin: CGPoint.zero, size: size1))
    image2.draw(in: CGRect(origin: origin, size: size2))

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
}

This is the output 这是输出

在此输入图像描述

To use this function to save to the photo album: 要使用此功能保存到相册:

@IBAction func save(_ sender: Any) {
    UIImageWriteToSavedPhotosAlbum(saveImage(image1:image1, image2:image2), 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)
    }
}

From this tutorial 本教程

The code you have in saveImage function should work - assuming that image1 is larger than image2, however, if image1 and image2 are the same sizes then you will only see the second image because the origin will become (0,0). 你在saveImage函数中的代码应该工作 - 假设 image1大于image2,但是,如果image1和image2的大小相同,那么你将只看到第二个图像,因为原点将变为(0,0)。 So in other words, if image1 and image2 are both 300x300, then image2 draws over image1. 换句话说,如果image1和image2都是300x300,那么image2将覆盖image1。

The IBAction method appears to be wired up correctly and I believe what you are missing is some logic to resize the second image. IBAction方法似乎正确连接,我相信你缺少的是调整第二个图像大小的一些逻辑。 Use the code below to guide you, the math will likely change depending on what ratio of image1 to image2 you're looking for. 使用下面的代码来指导您,数学可能会根据您正在寻找的image1与image2的比例而改变。

func cropImageIntoQuarterSquare(image: UIImage) -> UIImage? {
    let originalImageSize: CGSize = image.size
    let smallImageSize = CGSize(width: (originalImageSize.width / 4), height: (originalImageSize.height / 4))

    UIGraphicsBeginImageContext(smallImageSize)
    image.draw(at: CGPoint.zero)
    let imageResult = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return imageResult
}

If you incorporate the resizing code then your IBAction might look something like this: 如果您合并了调整大小代码,那么您的IBAction可能如下所示:

@IBAction func save(_ sender: Any) {
    guard let croppedImage = cropImageIntoQuarterSquare(image: image2),
        let combinedImage = saveImage(image1: image1, image2: croppedImage) else {
            // handle the error
            return
    }

    UIImageWriteToSavedPhotosAlbum(combinedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}

func saveImage(image1: UIImage, image2: UIImage) -> UIImage? {
    let size1 = image1.size
    let size2 = image2.size
    let origin = CGPoint(x: size1.width - size2.width, y: size1.height - size2.height)

    UIGraphicsBeginImageContext(size1)
    image1.draw(in: CGRect(origin: CGPoint.zero, size: size1))
    image2.draw(in: CGRect(origin: origin, size: size2))
    let combinedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return combinedImage
}

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

Edit: Forgot to give credit to @Peter Tao for some of the code he already provided - and the link from hackingwithswift.com he provided. 编辑:忘了给@Peter Tao一些他已经提供的代码 - 以及他提供的hackingwithswift.com的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM