iOS Development UI Chapter-quartz2d use (screenshot)

Source: Internet
Author: User

iOS Development UI Chapter-quartz2d use (screenshot)

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:

////YYVIEWCONTROLLER.M//01-Screenshot////Created by Apple on 14-6-12.//Copyright (c) 2014 itcase. All rights reserved.//#import"YYViewController.h"#import"mbprogresshud+nj.h"@interface Yyviewcontroller () @property (weak, nonatomic) Iboutlet UIView*Contentview;-(Ibaction) Btnclick: (UIButton *) sender; @end @implementation Yyviewcontroller- (void) viewdidload{[Super Viewdidload];}-(Ibaction) Btnclick: (UIButton *) Sender {//two seconds delay saveDispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{        //Get the graphics context//Uigraphicsbeginimagecontext (self.view.frame.size);Uigraphicsbeginimagecontext (self.contentView.frame.size); //drawing a view into the graphics context//[Self.view.layer Renderincontext:uigraphicsgetcurrentcontext ()];[Self.contentView.layer Renderincontext:uigraphicsgetcurrentcontext ()]; //Save a screenshot to an albumUIImage *newimage=Uigraphicsgetimagefromcurrentimagecontext ();    Uiimagewritetosavedphotosalbum (Newimage,self, @selector (image:didFinishSavingWithError:contextInfo:), nil); });} - (voidImage: (UIImage *) image didfinishsavingwitherror: (nserror *) Error ContextInfo: (void*) contextinfo{if(Error) {[Mbprogresshud showerror:@"save failed, check to see if you have the appropriate permissions"]; }Else    {//[Mbprogresshud showmessage:@ "saved successfully! "];[Mbprogresshud showsuccess:@"Save success! "]; }} @end
Save the captured image to your phone's album:

Description:Draw the whole screen into a picture. 1. Create a bitmap context 2. Draws the screen in the context of 3. Remove the drawing picture from the context 4. Save pictures to albumsAdd:Code to write a picture to a file
// 3. Remove the drawn picture     from the context UIImage *newimage = uigraphicsgetimagefromcurrentimagecontext ();           *data = uiimagepngrepresentation (newimage);           *path = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) LastObject] stringByAppendingPathComponent:@ "abc.png"];     NSLog (@ "%@", path);     [Data Writetofile:path Atomically:yes];
Third, supplementaryWhat should I do after saving success and saving failures? System Recommended Method:
-(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error contextinfo: (void *) contextinfo{    if  (Error) {        [Mbprogresshud showerror:@ ' save failed, Please check if you have the relevant permissions "];    } Else     {//        [Mbprogresshud showmessage:@] saved successfully! "];        [Mbprogresshud showsuccess:@ " saved successfully! "];    }}
If the picture is saved successfully, the prompt is saved successfully. If the save fails, then the prompt failsTips:There are two common reasons for saving failures: 1 is not enough memory, and 2 is not allowed in the phone's internal permissions.Description:If an application wants to access an address book or album and the user has explicitly rejected it, then the subsequent visit will be rejected. This time, you can prompt the user to open permissions.

iOS Development UI Chapter-quartz2d use (screenshot)

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.