Difference between UIImage and UIImageView

2020-05-17 02:02发布

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

8条回答
The star\"
2楼-- · 2020-05-17 02:25

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

查看更多
Rolldiameter
3楼-- · 2020-05-17 02:27

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

UIImageView objects are used to display a UIImage

查看更多
SAY GOODBYE
4楼-- · 2020-05-17 02:29

UIImage is a data object that holds image bytes.

UIImageView is a control that display UIImage data.

查看更多
我只想做你的唯一
5楼-- · 2020-05-17 02:31
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.

查看更多
劫难
6楼-- · 2020-05-17 02:32

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
查看更多
淡お忘
7楼-- · 2020-05-17 02:35

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

查看更多
登录 后发表回答