简体   繁体   English

我应该在Swift iOS应用程序中使用UIImage或CGImage吗?

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

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. 加载jpg / png图像 - 可能是一个位图/ 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 可能将图像保存为压缩/调整大小的png / jpg文件
  • 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? 我是swift和ios平台的新手,但据我所知,cgimage尽可能接近? 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 eg UIImage, so I am now doubting my initial impression ha cgimage was the best match for my needs. 但是,当使用cgimage时,似乎没有办法从文件系统加载图像...但是我发现人们正在讨论UIImage的方法,所以我现在怀疑我最初的印象ha cgimage是最好的匹配我的需要。

It is easy to get confused between UIImage , CGImage and CIImage . UIImageCGImageCIImage之间很容易混淆。 The difference is following: 区别在于:

UIImage: UIImage object is a high-level way to display image data. UIImage: UIImage对象是一种显示图像数据的高级方式。 You can create images from files, from Quartz image objects, or from raw image data you receive. 您可以从文件, Quartz图像对象或您收到的原始图像数据创建图像。 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. 通常,您可以获取包含PNGJPEG表示图像的NSData对象,并将其转换为UIImage。

CGImage: A CGImage can only represent bitmaps. CGImage: CGImage只能表示位图。 Operations in CoreGraphics, such as blend modes and masking require CGImageRefs . CoreGraphics中的操作(例如混合模式和屏蔽)需要CGImageRefs If you need to access and change the actual bitmap data, you can use CGImage . 如果需要访问和更改实际的位图数据,可以使用CGImage It can also be converted to NSBitmapImageReps . 它也可以转换为NSBitmapImageReps

CIImage: A CIImage is an immutable object that represents an image. CIImage: CIImage是一个表示图像的不可变对象。 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 . 您通常将CIImage对象与其他Core Image类(如CIFilterCIContextCIColorCIVector 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. 您可以使用从各种来源(如Quartz 2D图像,核心视频图像等)提供的数据创建CIImage对象。需要使用各种GPU优化的Core Image过滤器。 They can also be converted to NSBitmapImageReps . 它们也可以转换为NSBitmapImageReps It can be based on the CPU or the GPU. 它可以基于CPU或GPU。

In conclusion, UIImage is what you are looking for. 总之, UIImage就是你要找的。 Reasons are: 原因是:

  1. You can get image from device memory and assign it to UIImage 您可以从设备内存中获取图像并将其分配给UIImage

  2. You can get image from URL and assign it to UIImage 您可以从URL获取图像并将其分配给UIImage

  3. You can write UIImage in your desired format to device memory 您可以将所需格式的UIImage写入设备内存

  4. You can resize image assigned to UIImage 您可以调整分配给UIImage的图像的大小

  5. Once you have assigned an image to UIImage, you can use that instance in controls directly. 将图像分配给UIImage后,可以直接在控件中使用该实例。 eg setting background of a button, setting as image for UIImageView 例如,设置按钮的背景,设置为UIImageView的图像

Would have added code samples but all these are basic questions which have been already answered on Stackoverflow, so there is no point. 本来会添加代码示例,但所有这些都是Stackoverflow上已经回答的基本问题,所以没有意义。 Not to mention adding code will make this unnecessarily large. 更不用说添加代码会使这不必要地变大。

Credit for summarizing differences: Randall Leung 总结差异的信用: Randall Leung

You can load your image easily into an UIImage object... 您可以轻松地将图像加载到UIImage对象中......

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

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

See more in UIImage documentation . UIImage文档中查看更多内容

Per the documentation at: https://developer.apple.com/documentation/uikit/uiimage 根据以下文档: https//developer.apple.com/documentation/uikit/uiimage

let uiImage = uiImageView.image
let cgImage = uiImage.cgImage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM