Ending scene paper ----- Quartz2D-imitating the system's UIImageView and uiimageview
Imitating the system's UIImageView
Overall Thinking:
To imitate the system's UIImageView, we must know how to use the system's UIView.
First usage
The usage of the system is to create a UIImageView object, set the frame, pass it a UIImage, and then add it to a View.
You can switch images.
Second usage
It is to directly pass a UIImage object during creation, and use the initWithImage method to create a UImageView.
The size of the UIImageView created in this way is the same as that of the original image.
Therefore, our own UIImageView should also have these features.
Steps:
Step 1: Create a New UIView named LLImageView.
Step 2: Add a UIImage attribute to LLImageView for external users to transmit images.
Step 3: Draw the passed image to the View in the DrawRect method.
The method for drawing is [_ image drawInRect: rect]. The size of the drawn image is the same as that of the UIView.
Step 4: rewrite the set Method of the UIImage attribute and re-draw the View in the set method. The objective is to switch the image.
Step 5: Provide an-(instancetype) initWithImage :( UIImage *) image method.
Override the init method in this method.
During initialization, make the View size as large as the actual size of the image.
Then assign a value to the UIImage attribute.
In this way, the displayed View has the same size as the actual size of the image.
Code implementation:
1 - (instancetype)initWithImage:(UIImage *)image{ 2 if (self = [super init]) { 3 self.frame = CGRectMake(0, 0, image.size.width, image.size.height); 4 _image = image; 5 } 6 return self; 7 } 8 9 10 -(void)setImage:(UIImage *)image{11 _image = image;12 [self setNeedsDisplay];13 }14 15 - (void)drawRect:(CGRect)rect {16 [_image drawInRect:rect];17 }