我应该在Swift iOS应用程序中使用UIImage或CGImage吗? [英] Should I use UIImage or CGImage in a Swift iOS App?

查看:132
本文介绍了我应该在Swift iOS应用程序中使用UIImage或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:


  • 加载jpg / png图像 - 可能是一个位图/ cgimage。 (他可以来自文件系统或来自在线下载的图像)

  • 可能将图像保存为压缩/调整大小的png / jpg文件

  • 为视觉控件提供图像参考

我是swift和ios平台的新手,但据我所知,cgimage是尽可能接近?但是,在使用cgimage时,似乎没有办法从文件系统加载图像......但是我发现有人在讨论例如UIImage,所以我现在怀疑我的初步印象ha cgimage是我最需要的。

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.

推荐答案

很容易得到混淆了 UIImage CGImage CIImage 。区别在于:

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

UIImage: UIImage 对象是高级别的显示图像数据的方式。您可以从文件, Quartz 图像对象或您收到的原始图像数据创建图像。它们是不可变的,必须在初始化时指定图像的属性。这也意味着可以从任何线程安全地使用这些图像对象。
通常你可以带 NSData 包含 PNG JPEG 表示图像并将其转换为UIImage。

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 只能代表位图。 CoreGraphics中的操作,例如混合模式和屏蔽需要 CGImageRefs 。如果需要访问和更改实际的位图数据,可以使用 CGImage 。它也可以转换为 NSBitmapImageReps

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 是一个表示图像的不可变对象。这不是一个图像。它只有与之关联的图像数据。它具有生成图像所需的所有信息。
您通常将 CIImage 对象与其他Core Image类结合使用,例如 CIFilter CIContext CIColor CIVector 。您可以使用从各种来源提供的数据创建CIImage对象,例如 Quartz 2D图像,核心视频图像等。
需要使用各种 GPU 优化的核心图像过滤器。它们也可以转换为 NSBitmapImageReps 。它可以基于CPU或GPU。

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.

总之, UIImage 就是你的意思正在找。原因是:

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


  1. 您可以从设备内存中获取图像并将其分配给UIImage

您可以从URL获取图像并将其分配给UIImage

你可以将所需格式的UIImage写入设备内存

您可以调整分配给UIImage的图像的大小

一旦您为UIImage分配了图像,就可以直接在控件中使用该实例。例如设置按钮的背景,设置为UIImageView的图像

会添加代码示例但所有这些是Stackoverflow上已经回答的基本问题,所以没有意义。更不用说添加代码会使这不必要地变大。

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.

总结差异的信用: Randall Leung

这篇关于我应该在Swift iOS应用程序中使用UIImage或CGImage吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆