How to Convert UIImage to CIImage and vice versa

Cocoa TouchIos5UiimageCore GraphicsCore Image

Cocoa Touch Problem Overview


I'm trying to get CIImage from ImageView that display on the screen.

UIImage *image = myImageView.image;

convert image UIImage to CIImage.

CIImage *cImage = [....];

How to do this?

Cocoa Touch Solutions


Solution 1 - Cocoa Touch

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!).

Solution 2 - Cocoa Touch

While Changxing Wang's answer is effectively correct, Apple says that UIImage.CIImage won't always work. Specifically, from PocketCoreImage:

// Create a CIImage from the _inputImage.  While UIImage has a property returning
// a CIImage representation of it, there are cases where it will not work.  This is the
// most compatible route.
_filteredImage = [[CIImage alloc] initWithCGImage:_inputImage.CGImage options:nil];

Apple also uses the following, which DOESN'T work for me:

[UIImage imageWithCIImage:_filteredImage] 

My personal implementation, adapted from this post does work for me:

// result is a CIImage, output of my filtering.  
// returnImage is (obviously) a UIImage that I'm going to return.
CIContext *context = [CIContext contextWithOptions:nil];
UIImage *returnImage = 
[UIImage imageWithCGImage:[context createCGImage:result fromRect:result.extent]];

Solution 3 - Cocoa Touch

This is most compatibile way of doing it:

UIImage* u =[UIImage imageNamed:@"image.png"];
CIImage* ciimage = [[CIImage alloc] initWithCGImage:u.CGImage];

now use ciimage...

and Swift version:

let i1 = UIImage(named: "testImage")
if let cgi1 = i1?.cgImage {
   let ci = CIImage(cgImage: cgi1)
   //use ci
}

(fixed typo in the if let statement)

Solution 4 - Cocoa Touch

Here is a complete code sample that I've used in my projects.

- (UIImage *)makeUIImageFromCIImage:(CIImage *)ciImage {
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgImage = [context createCGImage:ciImage fromRect:[ciImage extent]];
    
    UIImage* uiImage = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    
    return uiImage;
}

CIImage is basically a recipe for an image. You will run into problems if you try to directly convert from CIImage to UIImage. For example, calling

NSData * UIImageJPEGRepresentation (
   UIImage *image,
   CGFloat compressionQuality
);

will return nil.

Solution 5 - Cocoa Touch

func toCIImage(image: UIImage) -> CIImage {
    return image.CIImage ?? CIImage(CGImage: image.CGImage!)
}

func toUIImage(image: CIImage) -> UIImage {
    return UIImage(CIImage: image)
}

Solution 6 - Cocoa Touch

Swift 3/4

UIImage > CIImage

let ciimage = CIImage(image: image)

https://developer.apple.com/documentation/uikit/uiimage/1624090-init

CIImage > UIImage

let image = UIImage(ciImage: ciimage)

https://developer.apple.com/documentation/coreimage/ciimage/1624119-init

Solution 7 - Cocoa Touch

Swift example of changx's answer:

guard let ciimage = CIImage(image: uiimage) else { return }
let image = UIImage(CIImage: ciimage)

Solution 8 - Cocoa Touch

When converting UIImage to CIImage, I had 2 separate cases where it's sometimes nil and sometimes not. Below code solved it:

CIImage *ciImage;
ciImage=[[CIImage alloc] initWithImage:imageToConvert];
if (ciImage==nil)
    ciImage=imageToConvert.CIImage;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHelmiBView Question on Stackoverflow
Solution 1 - Cocoa TouchchangxView Answer on Stackoverflow
Solution 2 - Cocoa TouchChris Vander MeyView Answer on Stackoverflow
Solution 3 - Cocoa TouchJuraj AntasView Answer on Stackoverflow
Solution 4 - Cocoa TouchJVillellaView Answer on Stackoverflow
Solution 5 - Cocoa TouchEtanView Answer on Stackoverflow
Solution 6 - Cocoa Touchuser924View Answer on Stackoverflow
Solution 7 - Cocoa TouchDaniel StormView Answer on Stackoverflow
Solution 8 - Cocoa TouchLaurent CrivelloView Answer on Stackoverflow