How to Convert Ciimage to Uiimage in Swift 3.0

Unable to convert CIImage to UIImage in Swift 3.0

func convert(cmage:CIImage) -> UIImage
{
let context:CIContext = CIContext.init(options: nil)
let cgImage:CGImage = context.createCGImage(cmage, from: cmage.extent)!
let image:UIImage = UIImage.init(cgImage: cgImage)
return image
}

Use this function to convert CIImage to UIImage . It works .

CIImage Convert to UIImage

you will call init for resolve this issue

imgurl.image = UIImage.init(ciImage: transformedImage)

Create UIImage from CIImage on iOS 10

You can use following :

Objective C:

CIImage *ciImage = [blob image];
UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage];

Swift 4.0:

let ciImage: CIImage? = blob.image()
let uiImage = UIImage(ciImage: ciImage!)

CIImage to UIImage Distorts Color SWIFT

So my convert function was not correctly initializing the CIContext. Here's the corrected method:

  func convert(ciImage:CIImage) -> UIImage?
{
let context = CIContext(options: [CIContextOption.workingColorSpace: kCFNull!])
if let cgImage:CGImage = context.createCGImage(ciImage, from: ciImage.extent, format: .RGBA8, colorSpace: CGColorSpace.init(name: CGColorSpace.sRGB)!) {
let image:UIImage = UIImage.init(cgImage: cgImage)
return image
}
return nil
}

Unable to convert CIImage to UIImage in Swift 3.0

func convert(cmage:CIImage) -> UIImage
{
let context:CIContext = CIContext.init(options: nil)
let cgImage:CGImage = context.createCGImage(cmage, from: cmage.extent)!
let image:UIImage = UIImage.init(cgImage: cgImage)
return image
}

Use this function to convert CIImage to UIImage . It works .

How to Convert UIImage to CIImage and vice versa

CIImage *ciImage = [UIImage imageNamed:@"test.png"].CIImage;
UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage];

To fix the case where myUIImage.CIImage returns nil like [UIImageView image], you can instead do [CIImage imageWithCGImage:myUIImage.CGImage] – Dylan Hand

Swift version:

let ciImage = UIImage(named: "test.png")!.ciImage
let uiImage = UIImage(ciImage: ciImage)

To fix the case where myUIImage.ciImage returns nil like you can instead do CIImage(cgImage: myUIImage!.cgImage!).

Getting CIImage from UIImage (Swift)

If you are trying to work with filters, I worked around this playground bug:

let pic = UIImage(named: "crumpled.jpg")
let filter = CIFilter(name: "CISepiaTone")
filter.setValue(CIImage(image: pic), forKey: kCIInputImageKey)
filter.setValue(0.8, forKey: kCIInputIntensityKey)
let ctx = CIContext(options:nil)
let cgImage = ctx.createCGImage(filter.outputImage, fromRect:filter.outputImage.extent())


Related Topics



Leave a reply



Submit