0

I'm confused with when to use UIImage, CIImage and CGImage

What is the difference between them? When can I use them?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Tanya Rao
  • 11
  • 1
  • 5

2 Answers2

8

UIImage

Apple describes a UIImage object as a high-level way to display image data. You can create images from files, from Quartz image objects, or from raw image data you receive. They are immutable and must specify an image’s properties at initialization time. This also means that these image objects are safe to use from any thread. Typically you can take NSData object containing a PNG or JPEG representation image and convert it to a UIImage. To create a new UIImage, for example:

var newUIImage = UIImage(data: data)
//where data is a NSData

CIImage

A CIImage is a immutable object that represents an image. It is not an image. It only has the image data associated with it. It has all the information necessary to produce an image. You typically use CIImage objects in conjunction with other Core Image classes such as CIFilter, CIContext, CIColor, and CIVector. You can create CIImage objects with data supplied from variety of sources such as Quartz 2D images, Core Videos image, etc. It is required to use the various GPU optimized Core Image filters. They can also be converted to NSBitmapImageReps. It can be based on the CPU or the GPU. To create a new CIImage,

for example:

var newCIImage = CIImage(image: image)
//where image is a UIImage

CGImage

A CGImage can only represent bitmaps. Operations in CoreGraphics, such as blend modes and masking require CGImageRefs. If you need to access and change the actual bitmap data, you can use CGImage. It can also be converted to NSBitmapImageReps. To create a new UIImage from a CGImage,

for example:

var aNewUIImage = UIImage(CGImage: imageRef)
//where imageRef is a CGImage
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
Bhavesh Nayi
  • 705
  • 4
  • 15
0

They belong to different frameworks: UIKit, CoreImage and CoreGraphics respectively.

Normally, you use UIImage, unless you have a specific use case where other frameworks come in handy (such as using CoreImage filters).

Maxim Volgin
  • 3,957
  • 1
  • 23
  • 38