Swift: Convert between CGImage, CIImage and UIImage

From Hawk Wiki
Jump to: navigation, search

Swift: Convert CIImage to CGImage

func convertCIImageToCGImage(inputImage: CIImage) -> CGImage! {
    let context = CIContext(options: nil)
    if context != nil {
        return context.createCGImage(inputImage, fromRect: inputImage.extent())
    }
    return nil
}

Swift: Convert CGImage to CIImage

func convertCGImageToCIImage(inputImage: CGImage) -> CIImage! {
   var ciImage = CIImage(CGImage: inputImage)
    return ciImage
}

Swift: Convert CGImage or CIImage to UIImage

UIImage(CGImage: cgImage)
UIImage(CIImage: ciImage)

Swift: Convert UIImage to CIImage

//uiImage: UIImage
//Convert to CIImage
var ciImage = CIImage(image: uiImage)

Swift: Convert UIImage to CGImage

To convert to CGImage, it requires 2 steps:

First convert to CIImage #Swift: Convert UIImage to CIImage.

Then convert to CGImage #Swift:_Convert_CIImage_to_CGImage