How to convert UIImage to CIImage and vice versa

I am trying to get CIImage from an ImageView that is displayed on the screen.

 UIImage *image = myImageView.image; 

convert the UIImage image to CIImage .

 CIImage *cImage = [....]; 

How to do it?

+63
cocoa-touch ios5 core-graphics uiimage core-image
Dec 08 '11 at 3:10
source share
8 answers
 CIImage *ciImage = [UIImage imageNamed:@"test.png"].CIImage; UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage]; 

To fix the case where myUIImage.CIImage returns nil eg [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 as you can do CIImage(cgImage: myUIImage!.cgImage!) .

+120
Dec 08 '11 at 3:17
source share

While Changxing Wang's answer is indeed correct, Apple says UIImage.CIImage will not always work. In particular, 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 does NOT work for me:

 [UIImage imageWithCIImage:_filteredImage] 

My personal implementation adapted from this post works 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]]; 
+31
Jun 20 2018-12-12T00:
source share

This is the most compatible way to do this:

 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)

+20
Apr 24 '13 at 12:07 on
source share

Here is a complete sample of the code that I 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 an image recipe. You will have problems if you try to directly convert from CIImage to UIImage . For example, a call

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

nil will return.

+13
Mar 18 '15 at 13:54
source share
 func toCIImage(image: UIImage) -> CIImage { return image.CIImage ?? CIImage(CGImage: image.CGImage!) } func toUIImage(image: CIImage) -> UIImage { return UIImage(CIImage: image) } 
+11
Sep 07 '15 at 19:04
source share

Swift changx example :

 guard let ciimage = CIImage(image: uiimage) else { return } let image = UIImage(CIImage: ciimage) 
+4
Jun 16 '16 at 17:36
source share

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

+3
Mar 01 '18 at 14:22
source share

When converting UIImage to CIImage, I had 2 separate cases where it is sometimes zero and sometimes not. Below code solved this:

 CIImage *ciImage; ciImage=[[CIImage alloc] initWithImage:imageToConvert]; if (ciImage==nil) ciImage=imageToConvert.CIImage; 
+1
May 27 '18 at 18:53
source share



All Articles