在Swift中将图像保存在另一个图像的顶部 [英] Saving an image on top of another image in Swift

查看:89
本文介绍了在Swift中将图像保存在另一个图像的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Swift,我正在创建一个使用个人照片并将另一个放在其上的应用程序。我现在有一个hacky解决方案,创建该区域的屏幕截图并保存它。我需要在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

@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!

推荐答案

我建议您阅读这个主题。你的所有答案都在那里。阅读完本文后,以下代码示例可帮助您将两个图像正确合成。

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.

这篇关于在Swift中将图像保存在另一个图像的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆