iOS Development UI Chapter-quartz2d use (screenshot)

Source: Internet
Author: User

A simple explanation

In program development, it is sometimes necessary to intercept a piece of content on the screen, such as a fishing talent game.

Core code to complete the screenshot function:-(void) Renderincontext: (cgcontextref) ctx; Call the Renderincontext of a view's layer: method to

Second, code example

Storyboard Interface Construction:

Code:

 1//2//YYVIEWCONTROLLER.M 3//01-Screenshot 4//5//Created by Apple on 14-6-12. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" #import "mbprogresshud+nj.h" @interface Yyviewcontroller () @property (WEA K, nonatomic) Iboutlet UIView *contentview;14-(ibaction) Btnclick: (UIButton *) sender;15 @end17 @implementation Yyvi     EwController19-(void) viewDidLoad21 {[Super viewdidload];23}24-(ibaction) Btnclick: (UIButton *) Sender {26 27//Delay Two seconds Save Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0 * nsec_per_sec)), Dispatch_get_mai N_queue (), ^{29//Get graphics Context//Uigraphicsbeginimagecontext (self.view.frame.size); UIGRAPHICSB Eginimagecontext (self.contentView.frame.size); 32//drawing the view into the graphics context//[Self.view.layer rend Erincontext:uigraphicsgetcurrentcontext ()];35 [Self.contentView.layer renderincontext:   Uigraphicsgetcurrentcontext ()];36   37 38//Save screenshot to album UIImage *newimage=uigraphicsgetimagefromcurrentimagecontext (); 40 41     Uiimagewritetosavedphotosalbum (Newimage,self, @selector (image:didFinishSavingWithError:contextInfo:), nil); 42 });}44 (void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) contextInfo46 {if (error) {Mbprogresshud showerror:@ "save failed, please check if you have the relevant permissions"];49}else50 {wuyi//[mbprogress HUD showmessage:@ "saved successfully! "];52 [Mbprogresshud showsuccess:@" saved successfully! "];53}54}55 @end
Save the captured image to your phone's album: Description: Draw the entire screen into a picture 1. Create a bitmap context 2. Draws the screen in the context of the band 3. Remove the drawing picture from the context 4. Save picture to album add: Code to write a picture to a file
1//3. Remove the drawn picture from the context 2      UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext (); 3      4      nsdata *data = Uiimagepngrepresentation (newimage); 5      6      nsstring *path = [[Nssearchpathfordirectoriesindomains] ( NSDocumentDirectory, Nsuserdomainmask, YES) lastobject] stringbyappendingpathcomponent:@ "abc.png"];7 NSLog      (@ "% @ ", path); 8      [Data Writetofile:path Atomically:yes];
Third, supplementaryWhat should I do after saving success and saving failures? System Recommended Method:
1  -(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) ContextInfo 2 {3     if (Error) {4         [Mbprogresshud showerror:@ "Save failed, please check if you have the relevant permissions"]; 5     }else 6     {7//        [ Mbprogresshud showmessage:@ "saved successfully! "]; 8         [Mbprogresshud showsuccess:@ "saved successfully! "]; 9     }10}
If the picture is saved successfully, the prompt is saved successfully. If saving fails, the prompt fails: There are two common reasons for saving failures: 1 is insufficient memory, and 2 is not allowed inside the phone. Note: If an application wants to access the Address Book or album, the user has explicitly rejected it, then the subsequent visit will be rejected directly. This time, you can prompt the user to open permissions.

iOS Development UI Chapter-quartz2d use (screenshot)

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.