Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use UIImage or CGImage in a Swift iOS App?

Tags:

ios

cgimage

All the code I can find revolves around loading images directly into visual controls.

However, I have my own cache system (converting a project from another language) and so I as efficient as possible want the following:

  • Load jpg/png images - probably into a bitmap / cgimage. (his can either be from the file system or from images downloaded online)
  • Possibly save image back as a compressed/resized png/jpg file
  • Supply an image reference for a visual control

I am new to swift and ios platform, but as far as I can tell, cgimage is as close as it gets? However, there does not appear to be a way to load an image from he file system when using cgimage... But i have found people discussing ways for e.g. UIImage, so I am now doubting my initial impression ha cgimage was the best match for my needs.

like image 468
Tom Avatar asked Dec 29 '15 14:12

Tom


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

What is CGImage in Swift?

A bitmap image or image mask.

What is UIImage Swift?

An object that manages image data in your app.

How do you convert CIImage to UIImage?

This is most compatibile way of doing it: UIImage* u =[UIImage imageNamed:@"image. png"]; CIImage* ciimage = [[CIImage alloc] initWithCGImage:u. CGImage];


1 Answers

It is easy to get confused between UIImage, CGImage and CIImage. The difference is following:

UIImage: UIImage object is 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.

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.

CIImage: A CIImage is an 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.

In conclusion, UIImage is what you are looking for. Reasons are:

  1. You can get image from device memory and assign it to UIImage

  2. You can get image from URL and assign it to UIImage

  3. You can write UIImage in your desired format to device memory

  4. You can resize image assigned to UIImage

  5. Once you have assigned an image to UIImage, you can use that instance in controls directly. e.g. setting background of a button, setting as image for UIImageView

Would have added code samples but all these are basic questions which have been already answered on Stackoverflow, so there is no point. Not to mention adding code will make this unnecessarily large.

Credit for summarizing differences: Randall Leung

like image 122
NSNoob Avatar answered Oct 02 '22 04:10

NSNoob