iOS Development quartz2d Six draw Uiimageview

Source: Internet
Author: User

#import <UIKit/UIKit.h>@interface  xmgimageview:uiview/** * image;  -(Instancetype) Initwithimage: (UIImage *) image; @end
#import "XMGImageView.h"@implementationXmgimageview-(Instancetype) Initwithimage: (UIImage *) Image {if(self =[Super Init]) {        //determine the size of the current ImageViewSelf.frame = CGRectMake (0,0, Image.size.width, image.size.height); _image=image; }    returnSelf ;}-(void) SetImage: (UIImage *) Image {_image=image; //Redraw[self setneedsdisplay];}- (void) DrawRect: (cgrect) rect {//Drawing Code[Self.image Drawinrect:rect]; }@end
#import "ViewController.h"#import "XMGImageView.h"@interfaceViewcontroller ()/** < #注释 #>*/@property (nonatomic, weak) Uiimageview*Imagev, @property (nonatomic, weak) Xmgimageview*Xmgimagev;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //Uiimageview *imagev = [[Uiimageview alloc] init];//imagev.frame = CGRectMake (0, 0, $, +);//imagev.image = [UIImage imagenamed:@ "CTO"];//Self.imagev = Imagev;//[Self.view Addsubview:imagev]; //the size of the ImageView created by Initwithimage is as large as the original picture .//Uiimageview *imagev = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@ "CTO"];//[Self.view Addsubview:imagev];Xmgimageview*xmgimagev = [[Xmgimageview alloc] initwithimage:[uiimage imagenamed:@"CTO"]];    [Self.view Addsubview:xmgimagev]; ////Xmgimageview *xmgimagev = [[Xmgimageview alloc] init];//xmgimagev.frame = CGRectMake (0, 0, $, +);//xmgimagev.image = [UIImage imagenamed:@ "CTO"];//Self.xmgimagev = Xmgimagev;//[Self.view Addsubview:xmgimagev];        }-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event {        //self.imageV.image = [UIImage imagenamed:@ "Soda"];Self.xmgImageV.image = [UIImage imagenamed:@"Soda"];}@end

Overall idea:

We want to imitate the uiimageview of the system, we have to know how to use the UIView of the system.

The use of the system is to create a Uiimageview object, set the frame, pass a uiimage to it, and add it to a view.

You can toggle the picture.

This is the first usage.

The second usage is to pass a UIImage object directly at the time of creation, using the Initwithimage method to create a Uimageview way

The Uiimageview created with this approach is as large as the size of the original image.

So our own uiimageview have to have these functions as well.

Implementation steps:

The first step: Create a new UIView, named Xmgimageview.

Step Two: Add a uiimage attribute to Xmgimageview for the outside world to pass the picture

Step three: Draw the images passed to view in the DrawRect method

The drawing method is: [_image drawinrect:rect], drawing the size of the picture is as large as the size of the UIView.

Fourth step: Rewrite the set method of the UIImage property, and let the view redraw in the set method. Purpose in order to be able to switch pictures.

Fifth step: Provide A-(instancetype) Initwithimage: (UIImage *) Image method.

Rewrite the Init method in this method

When initializing, make the view size as large as the actual size of the picture.

Then assign a value to the UIImage property.

In this way, when the picture is drawn, the view shown is already size, and the size is as large as the actual size of the picture.

Specific code implementation:

-(Instancetype) Initwithimage: (UIImage *) image{

if (self = [super init]) {

Self.frame = CGRectMake (0, 0, image.size.width, image.size.height);

_image = image;

}

return self;

}

-(void) SetImage: (UIImage *) image{

_image = image;

[Self setneedsdisplay];

}

-(void) DrawRect: (cgrect) Rect {

[_image Drawinrect:rect];

}

iOS Development quartz2d Six draw Uiimageview

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.