IOSQuartz2D-04-manually crop images and save them to albums
Effect
Procedure
Draw a rectangle, and an alertView is displayed, prompting whether to save the image and clicking "yes". Save the image to the album and view the saved image in the album.
Implementation Add an imageView to the view of the controller, set the image to add a pan gesture to the view of the Controller to track the pan gesture, and draw a rectangle box (
Cut area of the image
At the end of the pan gesture, alertView prompts "do you want to save the image to the album ?"
Click "yes", save the image, and click "no". At present, no implementation steps are required.
Add an imageView (Set Images
In the. m file of the controller.
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
Set the cut area of the image drawn by gesture
Use the cut area of the image as the member attribute clipView
@property (nonatomic, weak) UIView *clipView;
Create a clipView through lazy loading and initialize it
-(UIView *) clipView {// if clipView is created, create if (_ clipView = nil) {UIView * view = [[UIView alloc] init]; _ clipView = view; // sets the background color and transparency view of clipView. backgroundColor = [UIColor blackColor]; view. alpha = 0.5; // Add the clipView to the Controller's view. At this time, the clipView is not displayed (its frame is not set) [self. view addSubview: _ clipView];} return _ clipView ;}
Add a pan gesture to the view of the controller, track the pan gesture, and draw an image cut area.
Create and add a gesture
/** Create gesture **/UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (pan :)]; /*** when the position of the pan gesture changes, the pan: method is called and the gesture is passed as a parameter * // ** add gesture **/[self. view addGestureRecognizer: pan];
Add member attributes to record the starting point of the pan gesture
@property (nonatomic, assign) CGPoint startPoint;
Listen for gesture Movement
-(Void) pan :( UIPanGestureRecognizer *) pan {CGPoint endPoint = CGPointZero; if (pan. state = UIGestureRecognizerStateBegan) {/** start point of the gesture when you start to click **/self. startPoint = [pan locationInView: self. view];} else if (pan. state = UIGestureRecognizerStateChanged) {/** when the gesture moves, the end point value is dynamically changed and the rectangular area between the start point and the end point is calculated. **/endPoint = [pan locationInView: self. view]; // calculate the width and height of the rectangle area CGFloat w = endPoint. x-self. startPoint. x; CGFloat h = EndPoint. y-self. startPoint. y; // calculate the frame CGRect clipRect = CGRectMake (self. startPoint. x, self. startPoint. y, w, h); // sets the frame self in the cut area. clipView. frame = clipRect;} else if (pan. state = UIGestureRecognizerStateEnded) {/** if the gesture is stopped, the image content in the cut area is drawn to the graphic context ** // enable the bitmap context uigraphicsbeginimagecontextwitexceptions (self. imageView. bounds. size, NO, 0); // create a closed path named UIBezierPath * path = [UIBezierPath bez IerPathWithRect: self. clipView. frame]; // set the content that exceeds the limit to not be displayed. [path addClip]; // obtain the drawing context CGContextRef context = UIGraphicsGetCurrentContext (); // set the image rendering context to [self. imageView. layer renderInContext: context]; // obtain the image UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); // disable the bitmap context UIGraphicsEndImageContext (); // remove the cut area view control, and clear [self. clipView removeFromSuperview]; self. clipView = nil; // display the image to the imageView. Self. imageView. image = image; // use alertView to indicate whether to save the image to the album UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ Save the image message: @ Save the image to the album? Delegate: self cancelButtonTitle: @ No otherButtonTitles: @ Yes, nil]; [alertView show] ;}}
Sets the proxy method of alertView to determine whether to save the image.
-(Void) alertView :( nonnull UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex {// if "yes" is clicked, the image if (buttonIndex = 1) is saved) {UIImageWriteToSavedPhotosAlbum (self. imageView. image, nil);/*** this method can be used to set the method for calling after saving. No setting is made here */}}