how to layer an image over another - ios

My code below kind of works but the frame is not symmetrical and I am using 2 images let bottomImage and let frame and only let frame should be used. To give you a example check out the image below I created. Frame should take the place of the black border.
#IBAction func Mask(_ sender: Any) {
let bottomImage:UIImage = UIImage(named: "backdropd")!
let frame:UIImage = UIImage(named: "ff")!
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)
let left:UIImage = imageTake.image!
left.draw(in: CGRect(x: newSize2.width/7,y: newSize2.height/8.9,width: newSize2.width/1,height: newSize2.height/1.29), blendMode:CGBlendMode.normal, alpha:1.0)
frame.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height: newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
imageTake.image = newImage
}

Related

How can I get spesific tableviewcell screenshot with navigationbar?

I'm trying to get screenshot of uitableviewcell and I can succesfully do it. But I cannot append the navigationbar top of the UIImage.
This is what i have:
https://i.imgur.com/8ULUFg5.jpg
This is what i want to get:
https://i.imgur.com/w0IpoYA.png
How can i append navigation bar to image?
I've tried to create new UIImage for create merged image but it doesn't work.
At the below there is my code for get cell's screenshot properly.
let cell = self.entryView.cellForRow(at: self.indexP) as! entryViewTableCell
let frame = cell.frame
let kalan = self.indexP.row % 2
cell.entryText.backgroundColor = (kalan == 0) ? Theme.cellFirstColor : Theme.cellSecondColor
cell.entryText.text = ""
UIGraphicsBeginImageContextWithOptions(cell.entryText.frame.size, true, 2)
cell.entryText.attributedText = self.entryler[self.indexP.row]
cell.entryText.textColor = Theme.labelColor
cell.entryText.tintColor = Theme.linkColor
cell.entryText.layer.render(in: UIGraphicsGetCurrentContext()!)
let snapshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
edit:
https://i.imgur.com/OR4gV6L.jpg
I can get this image with below code, but navigationbar opacity too low. How can I opaque it?
let cell = self.entryView.cellForRow(at: self.indexP) as! entryViewTableCell
let frame = cell.entryText.frame
let kalan = self.indexP.row % 2
cell.entryText.backgroundColor = (kalan == 0) ? Theme.cellFirstColor : Theme.cellSecondColor
cell.entryText.text = ""
UIGraphicsBeginImageContextWithOptions(frame.size, true, 2)
cell.entryText.attributedText = self.entryler[self.indexP.row]
cell.entryText.textColor = Theme.labelColor
cell.entryText.tintColor = Theme.linkColor
cell.entryText.layer.render(in: UIGraphicsGetCurrentContext()!)
let snapshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIGraphicsBeginImageContextWithOptions((self.navigationController?.navigationBar.layer.frame.size)!, true, 2)
self.navigationController?.navigationBar.drawHierarchy(in: (self.navigationController?.navigationBar.bounds)!, afterScreenUpdates: false)
self.navigationController?.navigationBar.layer.render(in: UIGraphicsGetCurrentContext()!)
let ss = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let img = self.getMixedImg(image1: ss!, image2: snapshot!)
func getMixedImg(image1: UIImage, image2: UIImage) -> UIImage? {
let size = CGSize(width: image1.size.width, height: image1.size.height + image2.size.height)
UIGraphicsBeginImageContextWithOptions(size, true, 2)
image1.draw(in: CGRect(x: 0,y: 0,width: size.width, height: image1.size.height))
image2.draw(in: CGRect(x: 0,y: image1.size.height,width: size.width, height: image2.size.height))
let finalImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return finalImage
}
This is answer for my question:
let cell = self.entryView.cellForRow(at: self.indexP) as! entryViewTableCell
let frame = cell.entryText.frame
let kalan = self.indexP.row % 2
cell.entryText.backgroundColor = (kalan == 0) ? Theme.cellFirstColor : Theme.cellSecondColor
cell.entryText.text = ""
UIGraphicsBeginImageContextWithOptions(frame.size, true, 3)
cell.entryText.attributedText = self.entryler[self.indexP.row]
cell.entryText.textColor = Theme.labelColor
cell.entryText.tintColor = Theme.linkColor
cell.entryText.layer.render(in: UIGraphicsGetCurrentContext()!)
let snapshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIGraphicsBeginImageContextWithOptions((self.navigationController?.navigationBar.layer.bounds.size)!, true, 3)
self.navigationController?.navigationBar.drawHierarchy(in: (self.navigationController?.navigationBar.layer.bounds)!, afterScreenUpdates: false)
let ss = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let img = self.getMixedImg(image1: ss!, image2: snapshot!)
func getMixedImg(image1: UIImage, image2: UIImage) -> UIImage? {
let size = CGSize(width: image1.size.width, height: image1.size.height + image2.size.height)
UIGraphicsBeginImageContextWithOptions(size, true, 2)
image1.draw(in: CGRect(x: 0,y: 0,width: size.width, height: image1.size.height))
image2.draw(in: CGRect(x: 0,y: image1.size.height,width: size.width, height: image2.size.height))
let finalImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return finalImage
}
and result: https://i.imgur.com/ezcfxnw.jpg

UIImage masking doesn't work (Swift, iOS 10)

Trying to mask an image with my custom mask. I think I follow the ideas correctly, but for some reason, image isn't get masked. Instead, masked image, created after masking, contains original cropped image as the mask wasn't applied.
Here's the Swift playground code which one can use in order to test my code (image and mask are attached, just drop them to the resources folder):
import UIKit
extension UIImage {
static func resizeImage(image: UIImage, width: CGFloat) -> UIImage {
let scale = width / image.size.width
let newHeight = round(image.size.height * scale)
UIGraphicsBeginImageContextWithOptions(CGSize(width:width, height:newHeight), false, image.scale)
image.draw(in: CGRect(origin: CGPoint(x:0, y:0), size: CGSize(width: width, height: newHeight)))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
static func resizeImage(image: UIImage, height: CGFloat) -> UIImage {
let scale = height / image.size.height
let newWidth = round(image.size.width * scale)
UIGraphicsBeginImageContextWithOptions(CGSize(width:newWidth, height:height), false, image.scale)
image.draw(in: CGRect(origin: CGPoint(x:0, y:0), size: CGSize(width: newWidth, height: height)))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
let image = UIImage(named: "image.jpg")!
var mask = UIImage(named: "mask.jpg")!
let k1 = image.size.width / image.size.height
let k2 = mask.size.width / mask.size.height
if k1 >= k2
{
mask = UIImage.resizeImage(image: mask, height: image.size.height)
}
else
{
mask = UIImage.resizeImage(image: mask, width: image.size.width)
}
image
mask
let center = CGPoint(x: image.size.width/2, y: image.size.height/2)
let croppingRect = CGRect(x: abs(image.size.width-mask.size.width)/2*image.scale,
y: abs(image.size.height-mask.size.height)/2*image.scale,
width: mask.size.width*image.scale,
height: mask.size.height*image.scale).integral
let maskReference = mask.cgImage!
let imageReference = image.cgImage!.cropping(to: croppingRect)!
let imageMask = CGImage(maskWidth: maskReference.width,
height: maskReference.height,
bitsPerComponent: maskReference.bitsPerComponent,
bitsPerPixel: maskReference.bitsPerPixel,
bytesPerRow: maskReference.bytesPerRow,
provider: maskReference.dataProvider!, decode: nil, shouldInterpolate: true)
imageMask?.colorSpace
imageMask?.alphaInfo
let maskedReference = imageReference.masking(imageMask!)
let maskedImage = UIImage(cgImage:maskedReference!, scale: image.scale, orientation: image.imageOrientation)
Swift 4+
let icon = UIImageView(image: YOURIMAGE)
icon.frame = CGRect(x:100, y: 100, width: 100, height: 100)
icon.layer.masksToBounds = true
let maskView = UIImageView()
maskView.image = YOURMASKIMAGE
maskView.frame = icon.bounds
icon.mask = maskView
icon.contentMode = .scaleToFill
icon.clipsToBounds = true
view.addSubview(icon)

how to save 2 images on top of each other (swift3)

My code right now just saves 1 image. I would like to save a smaller image in the top left corner above the first image. The big thing is just to make it save together in the photo gallery. 2 images on top of each other.
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
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()
All of this code goes into a different function to add it to a image view.
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

How do I draw on an image in Swift?

I need to be able to programmatically draw on an image, and save that image for later use. Say, draw a line on specific x and y coordinates on the image, save the image, and display it onto a simple view controller. How would I go about doing this in Swift? (Preferably Swift 2, I am still in development and haven't updated my mac to Sierra)
Update: Possibly something to do with converting a UIImage to a CGLayer, drawing on it, and then converting it back to a UIImage.
All you need to do is create and get an Image Context object and access all its powerful drawing methods. You can learn more about the CGContext object features here.
This function draws a line and a circle on an UIImage and returns the modified image:
Swift 4
func drawOnImage(_ image: UIImage) -> UIImage {
// Create a context of the starting image size and set it as the current one
UIGraphicsBeginImageContext(image.size)
// Draw the starting image in the current context as background
image.draw(at: CGPoint.zero)
// Get the current context
let context = UIGraphicsGetCurrentContext()!
// Draw a red line
context.setLineWidth(2.0)
context.setStrokeColor(UIColor.red.cgColor)
context.move(to: CGPoint(x: 100, y: 100))
context.addLine(to: CGPoint(x: 200, y: 200))
context.strokePath()
// Draw a transparent green Circle
context.setStrokeColor(UIColor.green.cgColor)
context.setAlpha(0.5)
context.setLineWidth(10.0)
context.addEllipse(in: CGRect(x: 100, y: 100, width: 100, height: 100))
context.drawPath(using: .stroke) // or .fillStroke if need filling
// Save the context as a new UIImage
let myImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// Return modified image
return myImage
}
It's simple:
Make an image graphics context. (Before iOS 10, you would do this by calling UIGraphicsBeginImageContextWithOptions. In iOS 10 there's another way, UIGraphicsImageRenderer, but you don't have to use it if you don't want to.)
Draw (i.e. copy) the image into the context. (UIImage actually has draw... methods for this very purpose.)
Draw your line into the context. (There are CGContext functions for this.)
Extract the resulting image from the context. (For example, if you used UIGraphicsBeginImageContextWithOptions, you would use UIGraphicsGetImageFromCurrentImageContext.) Then close the context.
Details
Xcode 9.1, Swift 4
Solution
extension UIImage
extension UIImage {
typealias RectCalculationClosure = (_ parentSize: CGSize, _ newImageSize: CGSize)->(CGRect)
func with(image named: String, rectCalculation: RectCalculationClosure) -> UIImage {
return with(image: UIImage(named: named), rectCalculation: rectCalculation)
}
func with(image: UIImage?, rectCalculation: RectCalculationClosure) -> UIImage {
if let image = image {
UIGraphicsBeginImageContext(size)
draw(in: CGRect(origin: .zero, size: size))
image.draw(in: rectCalculation(size, image.size))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
return self
}
}
extension UIImageView
extension UIImageView {
enum ImageAddingMode {
case changeOriginalImage
case addSubview
}
func drawOnCurrentImage(anotherImage: UIImage?, mode: ImageAddingMode, rectCalculation: UIImage.RectCalculationClosure) {
guard let image = image else {
return
}
switch mode {
case .changeOriginalImage:
self.image = image.with(image: anotherImage, rectCalculation: rectCalculation)
case .addSubview:
let newImageView = UIImageView(frame: rectCalculation(frame.size, image.size))
newImageView.image = anotherImage
addSubview(newImageView)
}
}
}
Images samples
Parent Image:
Child Image:
Usage example 1
func sample1(imageView: UIImageView) {
imageView.contentMode = .scaleAspectFit
imageView.image = UIImage(named: "parent")?.with(image: "child") { parentSize, newImageSize in
print("parentSize = \(parentSize)")
print("newImageSize = \(newImageSize)")
return CGRect(x: 50, y: 50, width: 90, height: 90)
}
}
Result 1
Usage example 2
func sample2(imageView: UIImageView) {
imageView.contentMode = .scaleAspectFit
imageView.image = UIImage(named: "parent")
imageView.drawOnCurrentImage(anotherImage: UIImage(named: "child"), mode: .changeOriginalImage) { parentSize, newImageSize in
print("parentSize = \(parentSize)")
print("newImageSize = \(newImageSize)")
let sideLength:CGFloat = 90
let indent:CGFloat = 50
return CGRect(x: parentSize.width-sideLength-indent, y: parentSize.height-sideLength-indent, width: sideLength, height: sideLength)
}
}
Result 2
Usage example 3
func sample3(imageView: UIImageView) {
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.image = UIImage(named: "parent")
imageView.drawOnCurrentImage(anotherImage: UIImage(named: "child"), mode: .addSubview) { parentSize, newImageSize in
print("parentSize = \(parentSize)")
print("newImageSize = \(newImageSize)")
let sideLength:CGFloat = 90
let indent:CGFloat = 15
return CGRect(x: parentSize.width-sideLength-indent, y: indent, width: sideLength, height: sideLength)
}
}
Result 3
Full sample code
Don't forget to add Solution code here
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView(frame: UIScreen.main.bounds)
view.addSubview(imageView)
sample1(imageView: imageView)
// sample2(imageView: imageView)
// sample3(imageView: imageView)
}
func sample1(imageView: UIImageView) {
imageView.contentMode = .scaleAspectFit
imageView.image = UIImage(named: "parent")?.with(image: "child") { parentSize, newImageSize in
print("parentSize = \(parentSize)")
print("newImageSize = \(newImageSize)")
return CGRect(x: 50, y: 50, width: 90, height: 90)
}
}
func sample2(imageView: UIImageView) {
imageView.contentMode = .scaleAspectFit
imageView.image = UIImage(named: "parent")
imageView.drawOnCurrentImage(anotherImage: UIImage(named: "child"), mode: .changeOriginalImage) { parentSize, newImageSize in
print("parentSize = \(parentSize)")
print("newImageSize = \(newImageSize)")
let sideLength:CGFloat = 90
let indent:CGFloat = 50
return CGRect(x: parentSize.width-sideLength-indent, y: parentSize.height-sideLength-indent, width: sideLength, height: sideLength)
}
}
func sample3(imageView: UIImageView) {
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.image = UIImage(named: "parent")
imageView.drawOnCurrentImage(anotherImage: UIImage(named: "child"), mode: .addSubview) { parentSize, newImageSize in
print("parentSize = \(parentSize)")
print("newImageSize = \(newImageSize)")
let sideLength:CGFloat = 90
let indent:CGFloat = 15
return CGRect(x: parentSize.width-sideLength-indent, y: indent, width: sideLength, height: sideLength)
}
}
}
Since iOS 10 you can use the UIGraphicImageRenderer, which has a better syntax and has some great features!
Swift 4
let renderer = UIGraphicsImageRenderer(size: view.bounds.size)
let image = renderer.image { context in
// draw your image into your view
context.cgContext.draw(UIImage(named: "myImage")!.cgImage!, in: view.frame)
// draw even more...
context.cgContext.setFillColor(UIColor.red.cgColor)
context.cgContext.setStrokeColor(UIColor.black.cgColor)
context.cgContext.setLineWidth(10)
context.cgContext.addRect(view.frame)
context.cgContext.drawPath(using: .fillStroke)
}
Updated Answer: Once you get the From and To coordinates, here is how to draw a line in a UIImage with those coordinates. From and To coordinates are in image pixels.
func drawLineOnImage(size: CGSize, image: UIImage, from: CGPoint, to: CGPoint) -> UIImage {
// begin a graphics context of sufficient size
UIGraphicsBeginImageContext(size)
// draw original image into the context
image.drawAtPoint(CGPointZero)
// get the context for CoreGraphics
let context = UIGraphicsGetCurrentContext()
// set stroking width and color of the context
CGContextSetLineWidth(context, 1.0)
CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor)
// set stroking from & to coordinates of the context
CGContextMoveToPoint(context, from.x, from.y)
CGContextAddLineToPoint(context, to.x, to.y)
// apply the stroke to the context
CGContextStrokePath(context)
// get the image from the graphics context
let resultImage = UIGraphicsGetImageFromCurrentImageContext()
// end the graphics context
UIGraphicsEndImageContext()
return resultImage }

merge two different images in Swift

I want to merge two UIImage in Swift:
and
I tried it with
func maskImage(image: UIImage, withMask maskImage: UIImage) -> UIImage {
let maskRef = maskImage.CGImage
let mask = CGImageMaskCreate(
CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef),
nil,
false)
let masked = CGImageCreateWithMask(image.CGImage, mask)
let maskedImage = UIImage(CGImage: masked!)
// No need to release. Core Foundation objects are automatically memory managed.
return maskedImage
}
and the call
let imageName = data.valueForKey("imagename")!.description
let image = UIImage(named: imageName)
let imageBackground : UIImage = UIImage(named:"background")!
let maskedImage: UIImage = self.maskImage(image!, withMask: imageBackground)
cell.imageButton.setImage(maskedImage, forState: .Normal)
the result is just the image that i get with let image = UIImage(named: imageName), the second image (volleyball)
What´s my error?
If you don't care about performance you can use Core Image
let volleyballImage = CIImage(image: UIImage(named:"volleyball.png")!)
let otherImage = CIImage(image: UIImage(named:"other.png")!)
let compositeFilter = CIFilter(name: "CIAdditionCompositing")!
compositeFilter.setValue(volleyballImage,
forKey: kCIInputImageKey)
compositeFilter.setValue(otherImage,
forKey: kCIInputBackgroundImageKey)
if let compositeImage = compositeFilter.outputImage{
let image = UIImage(CIImage: compositeImage)
// do something with the "merged" image
}
You can do it in few lines:
var bottomImage:UIImage = UIImage(named: "imageName")!
var topImage:UIImage = UIImage(named:"background")!
// Change here the new image size if you want
var newSize = CGSizeMake(bottomImage.size.width, bottomImage.size.height)
UIGraphicsBeginImageContextWithOptions(newSize, false, bottomImage.scale)
bottomImage.drawInRect(CGRectMake(0,0,newSize.width,newSize.height))
topImage.drawInRect(CGRectMake(0,0,newSize.width,newSize.height), blendMode:CGBlendMode.Normal, alpha:1.0)
var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
You can use my simple method to merge two images in half.
Like this:
func mergeTwoImage(leftImg:UIImage,rightImg:UIImage) -> UIImage?{
let size = CGSize(width: leftImg.size.width, height: leftImg.size.height)
UIGraphicsBeginImageContext(size)
let area1 = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let area2 = CGRect(x: size.width/2, y: 0, width: size.width, height: size.height)
leftImg.draw(in: area1)
rightImg.draw(in: area2, blendMode: .normal, alpha: 1)
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return finalImage
}

Resources