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