OC, oc Language

Source: Internet
Author: User

OC, oc Language
Effect

  • Procedure

    • Draw a rectangle and a alertView is displayed, prompting whether to save the image
    • Click "yes" to save the image to the album.
    • View saved images in the album
Implementation
  • Add an imageView to the Controller view to set the image
  • Add a pan gesture to the view of the controller.
  • Track the pan gesture and draw a rectangular 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" to save the image.
    • Click "no" and do nothing for now
Steps
  • Add an imageView (Set ImagesIn 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 = endPoin T. 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 bezierPathWithRe Ct: 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 imageView self. imageVie W. image = image; // use alertView to indicate whether to save the image to the album UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "Save image" message: @ "do you want to 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 */}}

     

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.