[翻译]  Saving an image on top of another image in Swift

[CHINESE]  在Swift中将图像保存在另一个图像的顶部


I am learning Swift and I am creating an app that uses a personal photo and puts another on top of it. I now have a hacky solution, to create a screenshot of the area and save it. I need to do this in Swift

我正在学习Swift,我正在创建一个使用个人照片并将另一个放在其上的应用程序。我现在有一个hacky解决方案,创建该区域的屏幕截图并保存它。我需要在Swift中这样做

@IBAction func saveImage(sender: AnyObject) {


    //Create the UIImage
    UIGraphicsBeginImageContext(imageView.frame.size)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    //Save it to the camera roll
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)


}

But, this was working and isn't anymore. But, this is also not the best solution.

但是,这是有效的,现在已经不复存在了。但是,这也不是最好的解决方案。

So guys, how can I save an image to the camera roll from a personal image, with an image as overlay?

那么伙计们,如何将图像从个人图像保存到相机胶卷,图像作为叠加?

Help would be greatly appreciated!! Thanks!

非常感谢帮助!!谢谢!

3 个解决方案

#1


20  

I would recommend reading through this thread. All your answers are there. Once you read through that article, the following code sample should help you composite the two images together properly.

我建议通过这个帖子阅读。你的所有答案都在那里。阅读完本文后,以下代码示例应该可以帮助您将两个图像正确地合成在一起。

func saveImage() {
    let bottomImage = UIImage(named: "bottom")!
    let topImage = UIImage(named: "top")!

    let newSize = CGSizeMake(100, 100) // set this to what you need
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)

    bottomImage.drawInRect(CGRect(origin: CGPointZero, size: newSize))
    topImage.drawInRect(CGRect(origin: CGPointZero, size: newSize))

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}

Hopefully this gets you going in the right direction.

希望这能让你朝着正确的方向前进。

#2


3  

Updated to Swift 3.0:

更新到Swift 3.0:

func saveImage() {
    let bottomImage = UIImage(named: "bottom")!
    let topImage = UIImage(named: "top")!

    let newSize = CGSizeMake(100, 100) // set this to what you need
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)

    bottomImage.draw(in: CGRect(origin: CGPointZero, size: newSize))//As drawInRect is deprecated
    topImage.draw(at: CGRect(origin: CGPointZero, size: newSize))//As drawInRect is deprecated

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}

#3


1  

UPDATE FOR SWIFT 4

SWIFT更新4

func saveImage() {
            let bottomImage = UIImage(named: "your bottom image name")!
            let topImage = UIImage(named: "your top image name")!

            let newSize = CGSize(width: 100, height: 100)   // set this to what you need
            UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)

            bottomImage.draw(in: CGRect(origin: CGPoint.zero, size: newSize))
            topImage.draw(in: CGRect(origin: CGPoint.zero, size: newSize))

            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
}

To use the image just refer to newImage

要使用图像,请参阅newImage

EXAMPLE HOW TO USE THE IMAGE:

示例如何使用图像:

 @IBOutlet weak var imageButton: UIButton!

imageButton.setBackgroundImage(newImage), for: .normal) 

This is an edit of cnoon's answer but optimized for Swift 4.

这是对cnoon答案的编辑,但针对Swift 4进行了优化。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号