This article introduces the iOS manual clipping pictures and save to the album details of the code, share for everyone to refer to, the specific content as follows
First, the realization effect
1. Operation steps
- Draws a rectangular box, pops up a alertview, prompts you to save the picture
- Click "Yes" to save the picture to the album
- View saved pictures in an album
2. Effect drawing
Second, the realization of ideas
1, in the Controller's view to add a ImageView, set the picture
2. Add a pan gesture to the view of the controller
3, tracking pan gesture, draw a rectangular box (picture of the shear area)
4, at the end of pan gesture, through the Alertview prompt "whether to save the picture to the album?" ”
- Click "Yes" to save the picture
- Click "No" and do nothing for the time being
Third, the realization step
1, through the storyboard in the controller's view to add a ImageView (set picture), and in the Controller's. m file to have this property
@property (Weak, nonatomic) Iboutlet Uiimageview *imageview;
2. Set the clipping area of the picture drawn by gesture
ClipView the clipping area of a picture as a member property
@property (nonatomic, weak) UIView *clipview;
3. Create ClipView by lazy loading and initialize
-(UIView *) ClipView
{
//If ClipView is created, create
if (_clipview = = nil)
{
UIView *view = [[UIView alloc] INIT];
_clipview = view;
Set ClipView background color and transparency
View.backgroundcolor = [Uicolor blackcolor];
View.alpha = 0.5;
Add ClipView to the controller's view, at which point the ClipView does not display (its frame is not set)
[Self.view Addsubview:_clipview];
}
return _clipview;
}
4. Add pan gesture to Controller view, follow pan gesture, draw picture clipping area
1), create and add gestures
/** Create gestures **/
uipangesturerecognizer *pan = [Uipangesturerecognizer alloc] initwithtarget:self action: @selector ( Pan:)];
/**
* Whenever the position of the pan gesture changes, the pan: method is invoked and the gesture is passed as a parameter/
/** Add gesture **/
[Self.view Addgesturerecognizer:pan ];
2, add member properties, record the point where the pan gesture begins
@property (nonatomic, assign) Cgpoint startpoint;
3), the movement of listening gestures
-(void) pan: (Uipangesturerecognizer *) Pan {cgpoint endPoint = Cgpointzero; if (pan.state = = Uigesturerecognizerstatebegan) {/** start clicking, record the start of gesture **/self.startpoint = [Pan locationinview:self
. view]; else if (pan.state = = uigesturerecognizerstatechanged) {/** dynamically changes the end value when the gesture moves, and calculates the rectangular area between the start and end points **/endPoint = [PA
n LocationInView:self.view];
Compute the width-height cgfloat w = endpoint.x-self.startpoint.x of the rectangular region;
CGFloat h = endpoint.y-self.startpoint.y;
The frame CGRect cliprect = CGRectMake (Self.startpoint.x, Self.startpoint.y, W, h) are computed for the rectangular region.
Sets the frame Self.clipView.frame = Cliprect for the clipping area; else if (pan.state = = uigesturerecognizerstateended) {/** If the gesture stops, draw the picture contents of the clipping area into the graphics context **///Open Bitmap context Uigraphi
Csbeginimagecontextwithoptions (self.imageView.bounds.size, NO, 0);
Creates a closed path size equal to the size of the clipping region uibezierpath *path = [Uibezierpath bezierPathWithRect:self.clipView.frame];
Set the content beyond the display, [path addclip]; Get the drawing context cgcontextref contexts = Uigraphicsgetcurrentcontext ();
In the context in which the picture is rendered [Self.imageView.layer renderincontext:context];
Gets the picture in the context uiimage *image = Uigraphicsgetimagefromcurrentimagecontext ();
Close Bitmap context uigraphicsendimagecontext ();
Remove the Cut area view control and empty [Self.clipview Removefromsuperview];
Self.clipview = nil;
Displays the picture to the imageview self.imageView.image = image; Prompts the user with Alertview to save the picture to the album Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Save the Picture" message:@ "Save the picture to the album ?
"Delegate:self cancelbuttontitle:@" "No" otherbuttontitles:@ "yes", nil];
[Alertview show];
}
}
4), set the Alertview proxy method to determine whether to save the picture
-(void) Alertview: (nonnull uialertview *) Alertview Clickedbuttonatindex: (nsinteger) buttonindex
{
//if clicked "Yes" , then save the picture
if (Buttonindex = = 1)
{
uiimagewritetosavedphotosalbum (self.imageView.image, nil, nil, nil);
/**
* This method can be set to save the call method, not set here
/
}
}
The above is the entire content of this article, I hope to help you learn.