xcode Swift 如何在图片上放置图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32105551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift how to place pictures on top of pictures
提问by Wivvle
I would like to make an app which enables you to take a photo and then choose from a set of pre made "pictures" as you will to apply on top of that photo.
我想制作一个应用程序,让您可以拍照,然后从一组预先制作的“图片”中进行选择,然后将其应用到该照片之上。
For example, you take a photo of someone and then apply a mustage, a chicken in it and fake lips.
例如,你给某人拍一张照片,然后涂上芥末、里面的一只鸡和假嘴唇。
App example is Aokify app.
应用示例是 Aokify 应用。
However searched all corners of the internet but can't find an example that points me in the right direction.
然而,搜索了互联网的各个角落,但找不到一个能指出我正确方向的例子。
采纳答案by Lytic
Another more simple implementation may be to use a UIImageView as a parent view, then add a UIImageView as a subview for any images you wish to overlay on top of the original.
另一个更简单的实现可能是使用 UIImageView 作为父视图,然后添加一个 UIImageView 作为您希望覆盖在原始图像之上的任何图像的子视图。
let mainImage = UIImage(named:"main-pic")
let overlayImage = UIImage(named:"overlay")
var mainImageView = UIImageView(image:mainImage)
var overlayImageView = UIImageView(image:overlayImage)
self.view.addSubview(mainImageView)
mainImageview.addSubview(overlayImageView)
Edit: Since this has become the accepted answer, I feel it is worth mentioning that there are also different options for positioning the overlayImageView: you can add the overlay to the same parent after the first view has been added, or you can add the overlay as a subview of the main imageView as the example demonstrates.
编辑:既然这已经成为公认的答案,我觉得值得一提的是,定位overlayImageView也有不同的选择:您可以在添加第一个视图后将覆盖添加到同一个父级,也可以添加覆盖作为示例演示的主 imageView 的子视图。
The difference is the frame of reference when setting the coordinates for your overlay frame: whether you want them to have the same coordinate space, or whether you want the overlay coordinates to be relative to the main image rather than the parent.
不同之处在于设置覆盖框架坐标时的参考框架:您是否希望它们具有相同的坐标空间,或者您是否希望覆盖坐标相对于主图像而不是父图像。
回答by Nikhil Manapure
For answering the question properly and fulfilling the requirement, you will need to add option for moving and placing the overlay image at proper position according to the original image but the code for adding one image over another image will be the following one-
为了正确回答问题并满足要求,您需要添加根据原始图像移动和放置叠加图像的选项,但是将一个图像添加到另一个图像上的代码如下-
For Swift3
对于Swift3
extension UIImage {
func overlayed(with overlay: UIImage) -> UIImage? {
defer {
UIGraphicsEndImageContext()
}
UIGraphicsBeginImageContextWithOptions(size, false, scale)
self.draw(in: CGRect(origin: CGPoint.zero, size: size))
overlay.draw(in: CGRect(origin: CGPoint.zero, size: size))
if let image = UIGraphicsGetImageFromCurrentImageContext() {
return image
}
return nil
}
}
Usage-
用法-
image.overlayed(with: overlayImage)
Also available here as a gist.
也可在此处作为要点使用。
The code was originally written to answer this question.
该代码最初是为了回答这个问题而编写的。
回答by Wivvle
Thanks to jesses.co.tt for providing the hint i needed.
感谢 jeesses.co.tt 提供我需要的提示。
The method is called UIGraphicsContext.
该方法称为 UIGraphicsContext。
And the tutorial i finally found that did it: https://www.youtube.com/watch?v=m1QnT72I6f0it's by thenewboston.
我终于发现教程做到了:https: //www.youtube.com/watch?v=m1QnT72I6f0 由thenewboston 提供。