IOS Drawing (drawrect) Picture cropped red box

Source: Internet
Author: User
Tags uikit

A red box is displayed in real time as your finger slides on the screen, which you can use to crop the picture. Create a new UIView class that inherits from the UIView and makes a drawing operation inside. The proxy method can be implemented in the required Uiviewcontroller. Picture clipping method, will be introduced in detail later, here do not explain. and code as follows:



MyView.h

Avatar Editor

Created by 15/5/8.

Copyright (c) years Dong. All rights reserved.

#import <UIKit/UIKit.h>

// Agent biography Pictures

@protocol geteditedimagedelegate <nsobject>


-(void) Getnewimage: (cgrect) newrect;


@end


@interface MyView:UIView

@property (nonatomic,strong) uiimageview *imageview;

Image

@property (nonatomic,strong) UIImage *myimage;

@property (nonatomic,assign)ID<geteditedimagedelegate> Getimagedelegate;

@end




myview.m

Avatar Editor

Created by 15/5/8.

Copyright (c) years Dong. All rights reserved.

#import "MyView.h"

@implementation MyView

{

Cgpoint Firsttouch;

Cgpoint Lasttouch;

Cgpoint LastTouch1;

}


-(instancetype) initWithFrame: (CGRect) frame

{

self = [superinitwithframe: frame];

if (self) {

}

return self;

}


// start touch

-(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event

{

uitouch *touch = [touches anyobject];

// get touch points

Firsttouch = [Touchlocationinview:self];

}


-(void) touchesmoved: (nsset *) touches withevent: (uievent *) event

{

uitouch *touch = [touches anyobject];

Lasttouch = [Touchlocationinview:self];

/**

* The shape between the starting point and the user finger drag point is plotted in real time

*/

[self setneedsdisplay];

}


-(void) touchesended: (nsset *) touches withevent: (uievent *) event

{

uitouch *touch = [touches anyobject];

Lasttouch = [Touchlocationinview:self];

[self setneedsdisplay];

cgrect rec = cgrectmake(firsttouch. X,firsttouch. Y,lasttouch. X -firsttouch. X,lasttouch. Y -firsttouch. Y);

/**

*

* Get an agent method for cropping picture size

* */

[self. Getimagedelegategetnewimage: rec];

}


// Get Rectangle

-(cgrect) currect

{


returncgrectmake(firsttouch. X,firsttouch. Y,lasttouch. X -firsttouch. X,lasttouch. Y -firsttouch. Y);

}


-(void) DrawRect: (cgrect) Rect {

//Drawing code

// Get the device context view as Canvas

Cgcontextref context = Uigraphicsgetcurrentcontext ();

//need to display the Red Box

Cgcontextsetstrokecolorwithcolor (context, [Uicolor Redcolor]. Cgcolor);

Cgcontextstrokerect (context, [ self currect]);

}

@end




IOS Drawing (drawrect) Picture cropped red box

Related Article

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.