原文地址:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/index.html#//apple_ref/occ/cl/UIImage
UIImage
继承自NSObject 需要导入UIKit库,要在iOS2.0和以后才能使用
A UIImage
object manages image data in your app. You use image objects to represent image data of all kinds, and the UIImage
class is capable of managing data for all image formats supported by the underlying platform. Image objects are immutable, so you always create them from existing image data, such as an image file on disk or programmatically created image data. An image object may contain a single image or a sequence of images you intend to use in an animation.
You can use image objects in several different ways:
-
Assign an image to a
UIImageView
object to display the image in your interface. -
Use an image to customize system controls such as buttons, sliders, and segmented controls.
-
Draw an image directly into a view or other graphics context.
-
Pass an image to other APIs that might require image data.
Although image objects support all platform-native image formats, it is recommended that you use PNG or JPEG files for most images in your app. Image objects are optimized for reading and displaying both formats, and those formats offer better performance than most other image formats. Because the PNG format is lossless, it is especially recommended for the images you use in your app’s interface.
一个UIImage对象管理你app中的图像数据。你可以使用图片对象来展示所有种类的图片数据,并且UIImage类是一个能够管理底层平台支持的所有图像格式的数据。图片对象是不可变的,图像对象是不可变的,所以你创造他们总是从现有的图像数据,如磁盘或以编程方式创建的图像数据。一个图像对象可以包含一个图像或一系列图像,你打算在一个动画中使用。
你可以用几种不同的方式来使用图像对象:
将image赋值给一个UIImageView对象来展示出来在你的界面。
使用一个image来自定义系统的控件如按钮,滑动条或者segmented controls(不知道叫什么好)。
直接画一个图片或者当其他图片的背景。
把一个image传给其他的需要传递这样参数的APIs.
虽然图片对象支持所有平台个是,但是建议在你的app中你尽量使用PNG和 JPEG格式图片。这两种图片提供了更好的性能来读和展示比大多数其他的图像格式。由于PNG格式是无损的,这就是为什么特别推荐的图像中使用你的应用程序的界面。
Creating Image Objects
When creating image objects using the methods of this class, you must have existing image data located in a file or data structure. You cannot create an empty image and draw content into it. There are many options for creating image objects, each of which is best for specific situations:
-
Use the
imageNamed:inBundle:compatibleWithTraitCollection:
method (or theimageNamed:
method) to create an image from an image asset or image file located in your app’s main bundle (or some other known bundle). Because these methods cache the image data automatically, they are especially recommended for images that you use frequently. -
Use the
imageWithContentsOfFile:
orinitWithContentsOfFile:
method to create an image object where the initial data is not in a bundle. These methods load the image data from disk each time, so you should not use them to load the same image repeatedly. -
Use the
animatedImageWithImages:duration:
andanimatedImageNamed:duration:
methods to create a singleUIImage
object comprised of multiple sequential images. Install the resulting image in aUIImageView
object to create animations in your interface.当创建图片对象时用这个类(UIImage),你必须有已经存在的图片数据在文件或数据结构中。你不能创建和画一个空的图片对象(必须提前引入图片)。创建图片对象的方式有很多,每个都有对应的最好的情况(不同情况使用不同的方法):
使用imageNamed:inBundle:compatibleWithTraitCollection:方法(或者
imageNamed:
方法)来创建一个图片从image asset 或者在你程序中的文件(或者一些已知的文件(不太理解这是什么意思~~))。因为这些方法自动缓存图片数据,它们是特别推荐常用的方法。使用
imageWithContentsOfFile:
或initWithContentsOfFile:方法来创建一个图片对象当数据不在一个包里。这些方法每次从磁盘中加在图片数据,所以你不应该重复的加载同一个图片。(就是加载一个图片次数少就用这个方法,如果次数太多,经常加载就用上边那个方法)。
使用
animatedImageWithImages:duration:
和animatedImageNamed:duration:
方法来创建一个图片对象由多个连续的图像组成来使你的页面动起来。
Other methods of the UIImage
class let you create animations from specific types of data, such as Core Graphics images or image data that you create yourself. UIKit also provides the UIGraphicsGetImageFromCurrentImageContext
function for creating images from content that you draw yourself. You use that function in conjunction with a bitmap-based graphics context, which you use to capture your drawing commands.
NOTE
Because image objects are immutable, you cannot change their properties after creation. Most image properties are set automatically using metadata in the accompanying image file or image data. The immutable nature of image objects also means that they are safe to use from any thread.
其他创建方法让你创建特定类型的数据的动画,例如 Core Graphics images或者你自己创建的图片数据。UIKit同事提供了UIGraphicsGetImageFromCurrentImageContext方法来创建你自己画的图片。你可以使用该函数与基于位图的图形环境相结合,可以使用根据绘图命令执行绘图。
注意:因为图像对象是不可改变的,所以你不能在创建后改变它们的属性。大多数图像属性被设置在所附的图像文件或图像数据中自动使用元数据。图像对象的不可改变的性质也意味着它们在任何线程中都是安全的。
Image assets are the easiest way to manage the images that ship with your app. Each new Xcode project contains an assets library, to which you can add multiple image sets. An image set contains the variations of a single image that your app uses. A single image set can provide different versions of an image for different platforms, for different trait environments (compact or regular), and for different scale factors.
In addition to loading images from disk, you can ask the user to supply images from an available camera or photo library using a UIImagePickerController
object. An image picker displays a custom user interface for selecting images. Accessing user-supplied images requires explicit user permission. For more information about using an image picker, see UIImagePickerController Class Reference.
除了从磁盘加载的图像,你可以让用户从可用的相机或照片库使用 UIImagePickerController
对象的图像提供。选择器选择图像显示一个自定义用户界面。访问用户提供的图像需要明确的用户权限。有关使用图像选择器的更多信息,参考UIImagePickerController Class Reference. (需要经过用户同意后才能打开图片选择)。
Defining a Stretchable Image
A stretchable image is one that defines regions where the underlying image data can be duplicated in an aesthetically pleasing way. Stretchable images are commonly used to create backgrounds that can grow or shrink to fill the available space.
You define a stretchable image by adding insets to an existing image using the resizableImageWithCapInsets:
or resizableImageWithCapInsets:resizingMode:
method. The insets subdivide the image into two or more parts. Specifying nonzero values for each inset yields an image divided into nine parts, as shown in Figure 1.
一个可以拉伸的图片是一个定义区域是潜在的图片数据可以重复以较美观的方式。拉伸图片通常用来填充创建的增大或缩小可用背景。
你可以定义一个可以拉伸的图片通过添加现有的图片使用resizableImageWithCapInsets:
或 resizableImageWithCapInsets:resizingMode:方法。
添加的图像细分为两个或多个部分。指定没部分插入非零值将图片分为9部分,就像图1.
![image: ../Art/image_insets_2x.png](https://i-blog.csdnimg.cn/blog_migrate/7a7067e0fd3ea7522ee0d8573b57698b.png)
Each inset defines the potion of the image that does not stretch in the given dimension. The regions inside an image’s top and bottom insets maintain a fixed height, and the areas inside the left and right insets maintain a fixed width. Figure 2 shows how each part of a nine-part image stretches as the image itself is stretched to fill the available space. The corners of the image do not change size because they are inside both a horizontal and vertical inset.
每插入的点的尺寸在纬度内不拉伸。在图像的顶部和底部插入保持固定高度的区域,在左侧和右侧的插图领域保持一个固定的宽度。图2展示如何将九分之一部分图像的每一部分延伸为图像本身,以填充可用空间。边角的图片不改变大小因为它们里边横向和纵向填充。
Figure 2Stretchable portions of a nine-part image![image: ../Art/image_stretching_2x.png](https://i-blog.csdnimg.cn/blog_migrate/eb6ca0326166d9abe2a51c583b7095fb.png)
Comparing Images
The isEqual:
method is the only reliable way to determine whether two images contain the same image data. The image objects you create may be different from each other, even when you initialize them with the same cached image data. The only way to determine their equality is to use the isEqual:
method, which compares the actual image data. Listing 1 illustrates the correct and incorrect ways to compare images.
OBJECTIVE-C
-
// Load the same image twice.
-
UIImage*image1= [UIImageimageNamed:@"MyImage"];
-
UIImage*image2= [UIImageimageNamed:@"MyImage"];
-
-
// The image objects may be different, but the contents are still equal
-
if([image1isEqual:image2]){
-
// Correct. This technique compares the image data correctly.
-
}
-
-
if(image1==image2){
-
// Incorrect! Direct object comparisons may not work.
-
}
Accessing the Image Data
Image objects not provide direct access to their underlying image data. However, you can retrieve the image data in other formats for use in your app. Specifically, you can use the CGImage
and CIImage
properties to retrieve versions of the image that are compatible with Core Graphics and Core Image, respectively. You can also use the UIImagePNGRepresentation
and UIImageJPEGRepresentation
functions to generate an NSData
object containing the image data in either the PNG or JPEG format.
UIImagePNGRepresentation
和 UIImageJPEGRepresentation方法来生成一个PNG或JPEG格式数据。