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

前端 未结 3 1004
感动是毒
感动是毒 2020-12-14 23:27

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

相关标签:
3条回答
  • 2020-12-15 00:02

    Per the documentation at: https://developer.apple.com/documentation/uikit/uiimage

    let uiImage = uiImageView.image
    let cgImage = uiImage.cgImage
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-15 00:25

    You can load your image easily into an UIImage object...

    NSData *data = [NSData dataWith...];
    UIImage *image = [UIImage imageWithData:data];
    

    If you then want to show it in a view, you can use an UIImageView:

    UIImageView *imageView = [[UIImageView alloc] init]; // or whatever
    ...
    imageView.image = image;
    

    See more in UIImage documentation.

    0 讨论(0)
提交回复
热议问题