Ios development tips: Shake screenshots

Source: Internet
Author: User

1. Monitor the shake Action 1> make the current view controller the first responder // you must first make the current view controller the first responder to respond to the action time [self becomeFirstResponder]; 2> implement response method-inherit from UIResponder method copy code 1-(void) motionBegan :( UIEventSubtype) motion withEvent :( UIEvent *) event2 {3 // if the action type is shake [shake] 4 if (motion = UIEventSubtypeMotionShake) {5 6 // call screenshot Method 7 [self snapshot]; 8} 9} copy code 2. screenshot Note: 1> when obtaining an image, you must enable the image context first, obtain the context 2> the method to be executed after being saved must be in a fixed format, that is, copy the code in the format shown in the following code 1 # pragma mark-click screenshot 2-(IBAction) snapshot 3 {4 // 1. enable image context [You must enable the context before proceeding to step 2. The sequence cannot be changed] 5 UIGraphicsBeginImageContext (self. view. bounds. size); 6 7 // 2. get context 8 CGContextRef context = UIGraphicsGetCurrentContext (); 9 10 // 3. render the current view layer to the current context 11 [self. view. layer renderInContext: context]; 12 13 // 4. obtain the image 14 UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); 15 16 // 5. disable image Context 17 UIGraphicsEndImageContext (); 18 19 // 6. save the image to the album 20 UIImageWriteToSavedPhotosAlbum (image, self, @ selector (image: didFinishSavingWithError: contextInfo :), nil ); 21} 22 23 # method called after the pragma mark is saved [fixed format] 24-(void) image :( UIImage *) image didFinishSavingWithError :( NSError *) error contextInfo :( void *) contextInfo25 {26 if (error) {27 NSLog (@ "error-% @", error. localizedDescription); 28} else {29 NSLog (@ "saved successfully"); 30} 31} copy the code

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.