Ending scene paper ----- Quartz2D-imitating the system's UIImageView and uiimageview

Source: Internet
Author: User

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  }

 

 





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.