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:

1 //2 //YYVIEWCONTROLLER.M3 //01-Screenshot4 //5 //Created by Apple on 14-6-12.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten #import "mbprogresshud+nj.h" One  A @interfaceYyviewcontroller () -@property (Weak, nonatomic) Iboutlet UIView *Contentview; --(Ibaction) Btnclick: (UIButton *) sender; the  - @end -  - @implementationYyviewcontroller +  -- (void) Viewdidload + { A [Super Viewdidload]; at } -  --(Ibaction) Btnclick: (UIButton *) Sender { -      -     //two seconds delay save -Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{ in         //Get the graphics context -         //Uigraphicsbeginimagecontext (self.view.frame.size); to Uigraphicsbeginimagecontext (self.contentView.frame.size); +         //drawing a view into the graphics context -          the         //[Self.view.layer Renderincontext:uigraphicsgetcurrentcontext ()]; * [Self.contentView.layer Renderincontext:uigraphicsgetcurrentcontext ()]; $      Panax Notoginseng          -         //Save a screenshot to an album theUIImage *newimage=Uigraphicsgetimagefromcurrentimagecontext (); +          A uiimagewritetosavedphotosalbum (newimage,self, @selector (image:didFinishSavingWithError:contextInfo:), nil); /c4> the     }); + } -  $- (voidImage: (UIImage *) image didfinishsavingwitherror: (nserror *) Error ContextInfo: (void*) ContextInfo $ { -     if(Error) { -[Mbprogresshud ShowError:@"save failed, check to see if you have the appropriate permissions"]; the}Else -     {Wuyi //[Mbprogresshud showmessage:@ "saved successfully! "]; the[Mbprogresshud showsuccess:@"Save success! "]; -     } Wu } -  About @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
1 //3. Remove the drawn picture from the context2UIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext ();3      4NSData *data =uiimagepngrepresentation (newimage);5      6NSString *path = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) LastObject] stringByAppendingPathComponent:@"Abc.png"];7NSLog (@"%@", path);8[Data Writetofile:path Atomically:yes];
Third, supplementaryWhat should I do after saving success and saving failures? System Recommended Method:
1- (voidImage: (UIImage *) image didfinishsavingwitherror: (nserror *) Error ContextInfo: (void*) ContextInfo2 {3     if(Error) {4[Mbprogresshud ShowError:@"save failed, check to see if you have the appropriate permissions"];5}Else6     {7 //[Mbprogresshud showmessage:@ "saved successfully! "];8[Mbprogresshud showsuccess:@"Save success! "];9     }Ten}
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.
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.