ios UIImage 和 UIImageView 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8070805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between UIImage and UIImageView
提问by ARC
What is the difference between UIImage
and UIImageView
? Can someone explain it with an example?
UIImage
和 和有UIImageView
什么区别?有人可以用一个例子来解释吗?
采纳答案by chown
Example:
例子:
UIImage *bgImage = [UIImage imageNamed:@"[email protected]"];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:bgImage];
backgroundImageView.frame = [[UIScreen mainScreen] bounds];
A
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. The UIImage class also offers several options for drawing images to the current graphics context using different blend modes and opacity values.Image objects are immutable, so you cannot change their properties after creation. This means that you generally specify an image's properties at initialization time or rely on the image's metadata to provide the property value. In some cases, however, the UIImage class provides convenience methods for obtaining a copy of the image that uses custom values for a property.
Because image objects are immutable, they also do not provide direct access to their underlying image data. However, you can get an NSData object containing either a PNG or JPEG representation of the image data using the
UIImagePNGRepresentation
andUIImageJPEGRepresentation
functions.The system uses image objects to represent still pictures taken with the camera on supported devices. To take a picture, use the UIImagePickerController class. To save a picture to the Saved Photos album, use the
UIImageWriteToSavedPhotosAlbum
function.
甲
UIImage
对象是一个高层次的方法来显示图像数据。您可以从文件、Quartz 图像对象或接收到的原始图像数据创建图像。UIImage 类还提供了几种使用不同混合模式和不透明度值将图像绘制到当前图形上下文的选项。图像对象是不可变的,因此您无法在创建后更改其属性。这意味着您通常在初始化时指定图像的属性或依赖图像的元数据来提供属性值。但是,在某些情况下,UIImage 类提供了获取使用自定义属性值的图像副本的便捷方法。
因为图像对象是不可变的,所以它们也不提供对其底层图像数据的直接访问。但是,您可以使用
UIImagePNGRepresentation
和UIImageJPEGRepresentation
函数获取包含图像数据的 PNG 或 JPEG 表示的 NSData 对象。系统使用图像对象来表示在支持的设备上使用相机拍摄的静态照片。要拍照,请使用 UIImagePickerController 类。要将图片保存到“保存的照片”相册,请使用该
UIImageWriteToSavedPhotosAlbum
功能。
An
UIImageView
provides a view-based container for displaying either a single image or for animating a series of images. For animating the images, theUIImageView
class provides controls to set the duration and frequency of the animation. You can also start and stop the animation freely.New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of
UIImageView
, you must explicitly change the value of theuserInteractionEnabled
property toYES
after initializing the object.When a
UIImageView
object displays one of its images, the actual behavior is based on the properties of the image and the view. If either of the image'sleftCapWidth
ortopCapHeight
properties are non-zero, then the image is stretched according to the values in those properties. Otherwise, the image is scaled, sized to fit, or positioned in the image view according to the contentMode property of the view. It is recommended (but not required) that you use images that are all the same size. If the images are different sizes, each will be adjusted to fit separately based on that mode.All images associated with a
UIImageView
object should use the same scale. If your application uses images with different scales, they may render incorrectly.
An
UIImageView
提供了一个基于视图的容器,用于显示单个图像或动画一系列图像。为了动画图像,UIImageView
该类提供控件来设置动画的持续时间和频率。您还可以自由启动和停止动画。默认情况下,新的图像视图对象被配置为忽略用户事件。如果要处理 的自定义子类中的事件
UIImageView
,则必须在初始化对象后显式更改userInteractionEnabled
属性的值YES
。当
UIImageView
对象显示其图像之一时,实际行为基于图像和视图的属性。如果图像leftCapWidth
或topCapHeight
属性中的任何一个不为零,则根据这些属性中的值拉伸图像。否则,图像将根据视图的 contentMode 属性缩放、调整大小以适合或定位在图像视图中。建议(但不要求)使用大小相同的图像。如果图像大小不同,每个图像将根据该模式分别调整以适合。与
UIImageView
对象关联的所有图像都应使用相同的比例。如果您的应用程序使用不同比例的图像,它们可能会呈现错误。
回答by Geekswordsman
UIImage
contains the data for an image.
UIImageView
is a custom view meant to display the UIImage
.
UIImage
包含图像的数据。
UIImageView
是一个自定义视图,用于显示UIImage
.
回答by user523234
In short:
You create an instance of UIImage
object to hold image's data, like this:
简而言之:您创建一个UIImage
对象实例来保存图像数据,如下所示:
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/picture.jpg"]; //assuming your image is in your app's bundle
UIImage *img = [[UIImage alloc]initWithContentsOfFile:sourcePath];
You then create an instance of UIImageView
either through IB or code to display your image on the screen, like this:
然后您UIImageView
通过 IB 或代码创建一个实例以在屏幕上显示您的图像,如下所示:
[imageView1 setImage:img]; //assume you already create an instance of UIImageView named imageView1
回答by eazimmerman
UIImage
objects store data from an image (i.e. data from a png file)
UIImage
对象存储来自图像的数据(即来自 png 文件的数据)
UIImageView
objects are used to display a UIImage
UIImageView
对象用于显示一个 UIImage
回答by logancautrell
UIImage
is a data object that holds image bytes.
UIImage
是一个保存图像字节的数据对象。
UIImageView
is a control that display UIImage
data.
UIImageView
是一个显示UIImage
数据的控件。
回答by Milosh Milivojevich
UIimage is an Object which stores data (Jpg, png...) and UIImageView class which contains UIImage as property. UIImageView can have multiple of UIImages, and UIImage is immutable. That bothered me long time ago, why when u create image you do: var image: UIImageView!
UIimage 是一个对象,它存储数据(Jpg、png...)和包含 UIImage 作为属性的 UIImageView 类。UIImageView 可以有多个 UIImage,而 UIImage 是不可变的。很久以前就困扰我了,为什么当你创建图像时你会这样做:var image: UIImageView!
but when u execute that "image" you do image.image = UIImage.init(named: "image") Its really easy, you use UIimageView, to create image with UIImage :) UIImage displays image, and UIImageView is container for that UIImage
但是当你执行那个“图像”你做 image.image = UIImage.init(named: "image") 它真的很容易,你使用 UIimageView,用 UIImage 创建图像:) UIImage 显示图像,而 UIImageView 是该 UIImage 的容器
回答by Kaviraj Pandey
UIImage holds the data as like (pictures) and displays it on UIImageView.
UIImage 将数据保存为(图片)并将其显示在 UIImageView 上。
UIImageView is a type of container to display images.
UIImageView 是一种显示图像的容器。
回答by kurupareshan
UIImageView *myImage = [[UIImageView alloc]init];
[myImage setImage:[UIImage imageNamed:@"1.png"]];
create an instance for UIImageView as "myImage".This is helps for dispaly data.UIImage helps for hold the data of 1.png from the assets file in the xcode.
为 UIImageView 创建一个实例为“myImage”。这有助于显示数据。UIImage 有助于从 xcode 中的资产文件中保存 1.png 的数据。