Difference between UIImage and UIImageView

雨燕双飞 提交于 2019-12-20 08:56:59

问题


What is the difference between UIImage and UIImageView? Can someone explain it with an example?


回答1:


Example:

UIImage *bgImage = [UIImage imageNamed:@"Default@2x.png"];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:bgImage];
backgroundImageView.frame = [[UIScreen mainScreen] bounds];

UIImage Overview:

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 and UIImageJPEGRepresentation 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.

UIImageView Overview:

An UIImageView provides a view-based container for displaying either a single image or for animating a series of images. For animating the images, the UIImageView 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 the userInteractionEnabled property to YES 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’s leftCapWidth or topCapHeight 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.




回答2:


UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage.




回答3:


In short: You create an instance of UIImage object to hold image's data, like this:

 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:

[imageView1 setImage:img];  //assume you already create an instance of UIImageView named imageView1



回答4:


UIImage objects store data from an image (i.e. data from a png file)

UIImageView objects are used to display a UIImage




回答5:


UIImage is a data object that holds image bytes.

UIImageView is a control that display UIImage data.




回答6:


UIImage holds the data as like (pictures) and displays it on UIImageView.

UIImageView is a type of container to display images.




回答7:


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.




回答8:


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!

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



来源:https://stackoverflow.com/questions/8070805/difference-between-uiimage-and-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!