4

How do I combine two or more CIImage into another one. I tried using ciContext.drawImage. How do I get a CGImage from it ?? What I have understood is that I am writing into a context from which I should be able to get an Image. Please let me know if I have understood anything wrong.

let eaglContext = EAGLContext(API: .OpenGLES2)
let ciContext = CIContext(EAGLContext: eaglContext)
ciContext.drawImage(firstCIImage, inRect: firstRect, fromRect: secondRect)
ciContext.drawImage(secondCIImage, inRect: secondRect, fromRect: secondRect)
Golak Sarangi
  • 809
  • 7
  • 22

2 Answers2

4

You may try something like that:

    var outputImage: CIImage?
    let images : Array<CIImage> = [] // add all your CIImages that you'd like to combine
    for image in images {
        outputImage = outputImage == nil ? image : image.imageByCompositingOverImage(outputImage!)
    }
    var cgImage: CGImageRef
    if let image = outputImage {
        let eaglContext = EAGLContext(API: .OpenGLES2)
        let ciContext = CIContext(EAGLContext: eaglContext)
        cgImage = ciContext.createCGImage(image, fromRect: image.extent)
    }
z0kovsky
  • 63
  • 1
  • 10
2

You should use CIFilter-s for that :)

tutorial

sloik
  • 694
  • 5
  • 12
  • I agree with sloik. Core Image Filters are the right tool for this task. – Duncan C Aug 13 '15 at 19:59
  • Sorry, I am still not able to do it. CIFilters need a image to be initialized. Which image should i initialise it with and what should be the filtername ? I have firstCIImage and secondCIImage and i want to combine it to form thirdCIImage. A code example will be very helpful – Golak Sarangi Aug 15 '15 at 00:29
  • http://stackoverflow.com/questions/14472415/xcode-compositing-with-alpha-using-core-image – sloik Aug 16 '15 at 13:30