您的位置:首页 > 产品设计 > UI/UE

UIImage

2016-05-23 11:47 483 查看
原文地址:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/index.html#//apple_ref/occ/cl/UIImageUIImage继承自NSObject 需要导入UIKit库,要在iOS2.0和以后才能使用A 
UIImage
 objectmanages image data in your app. You use image objects to represent image data of all kinds, and the 
UIImage
 classis 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 maycontain 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
 objectto 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 thanmost 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 isbest for specific situations:Use the 
imageNamed:inBundle:compatibleWithTraitCollection:
 method(or the 
imageNamed:
 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:
 or 
initWithContentsOfFile:
 methodto 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:
 and 
animatedImageNamed:duration:
 methodsto create a single 
UIImage
 objectcomprised of multiple sequential images. Install the resulting image in a 
UIImageView
 objectto create animations in your interface.当创建图片对象时用这个类(UIImage),你必须有已经存在的图片数据在文件或数据结构中。你不能创建和画一个空的图片对象(必须提前引入图片)。创建图片对象的方式有很多,每个都有对应的最好的情况(不同情况使用不同的方法):使用imageNamed:inBundle:compatibleWithTraitCollection:方法(或者
imageNamed:
 方法)来创建一个图片从imageasset 或者在你程序中的文件(或者一些已知的文件(不太理解这是什么意思~~))。因为这些方法自动缓存图片数据,它们是特别推荐常用的方法。使用
imageWithContentsOfFile:
 或 
initWithContentsOfFile:方法来创建一个图片对象当数据不在一个包里。这些方法每次从磁盘中加在图片数据,所以你不应该重复的加载同一个图片。(就是加载一个图片次数少就用这个方法,如果次数太多,经常加载就用上边那个方法)。
使用
animatedImageWithImages:duration:
 和 
animatedImageNamed:duration:
 方法来创建一个图片对象由多个连续的图像组成来使你的页面动起来。Other methods of the 
UIImage
 classlet 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
 functionfor 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.NOTEBecause 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 safeto 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 imageset 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 UIImagePickerControllerClass Reference. Image assets 是一个简单的方式来管理你的图片在你的app中。每个xcode项目包含一个assets库,可以放入不同的图片。一个image set包含一对一的图片。一个单一的图像集可以提供不同的版本的图像不同的平台,不同的特征环境(紧凑型或规则型),并为不同的规模尺寸。除了从磁盘加载的图像,你可以让用户从可用的相机或照片库使用 
UIImagePickerController
 对象的图像提供。选择器选择图像显示一个自定义用户界面。访问用户提供的图像需要明确的用户权限。有关使用图像选择器的更多信息,参考UIImagePickerControllerClass 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 Figure1.一个可以拉伸的图片是一个定义区域是潜在的图片数据可以重复以较美观的方式。拉伸图片通常用来填充创建的增大或缩小可用背景。你可以定义一个可以拉伸的图片通过添加现有的图片使用
resizableImageWithCapInsets:
 或 
resizableImageWithCapInsets:resizingMode:方法。
添加的图像细分为两个或多个部分。指定没部分插入非零值将图片分为9部分,就像图1.
 Figure1Using insets to define stretchable regionsEach 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. Figure2 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展示如何将九分之一部分图像的每一部分延伸为图像本身,以填充可用空间。边角的图片不改变大小因为它们里边横向和纵向填充。Figure2Stretchable portions of a nine-part image

Comparing Images

The 
isEqual:
 methodis 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 isto use the 
isEqual:
 method,which compares the actual image data. Listing 1 illustrates the correct and incorrect ways tocompare images. Listing1Comparing two imagesOBJECTIVE-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.
}
isEqual:方法是唯一可靠方法来区分图片是不是含有相同数据的。你创建的图片可能和其他的都不一样,即便是你初始化用的是相同数据。isEqual:是唯一能确定它们是不是相同的方法,当你比较两个图片的时候。例子1展示了比较两个图片正确和不正确的方法。

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
 propertiesto retrieve versions of the image that are compatible with Core Graphics and Core Image, respectively. You can also use the 
UIImagePNGRepresentation
 and 
UIImageJPEGRepresentation
 functionsto generate an 
NSData
objectcontaining the image data in either the PNG or JPEG format.Image对象没有提供能直接获取底层的图片数据的方法。然而,你可以取得图片数据以其他形式在你的app中。当然,你可以分别使用CGImageCIImage属性检索,核心图形核心图像兼容的图像版本。你也可以使用 
UIImagePNGRepresentation
 和 
UIImageJPEGRepresentation方法来生成一个PNG或JPEG格式数据。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uiimage image iOS