iOS screenshot, the basic method of view screenshot

Source: Internet
Author: User

iOS screenshot of the method online there are many, the following is I personally think the better one, I changed a little bit

Source: Sdscreenshotcapture

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO (v)  ([[[Uidevice Currentdevice] systemversion] compare:v options : nsnumericsearch]! = nsorderedascending)

UIImage *getimagewithfullscreenshot (void) {///Source (under MIT License): https://github.com/shinydevelopment/ Sdscreenshotcapture/blob/master/sdscreenshotcapture/sdscreenshotcapture.m#l35 BOOL ignoreOrientation = SYSTEM_VERS        Ion_greater_than_or_equal_to (@ "8.0");        uiinterfaceorientation orientation = [UIApplication sharedapplication].statusbarorientation;    Cgsize imageSize = Cgsizezero; if (uiinterfaceorientationisportrait (orientation) | | ignoreorientation) imageSize = [UIScreen mainscreen].bounds.si    Ze        else imageSize = Cgsizemake ([UIScreen mainscreen].bounds.size.height, [UIScreen mainscreen].bounds.size.width);    Uigraphicsbeginimagecontextwithoptions (ImageSize, NO, [UIScreen Mainscreen].scale);        Cgcontextref context = Uigraphicsgetcurrentcontext ();        For (UIWindow *window in [[[UIApplication sharedapplication] windows]) {cgcontextsavegstate (context); CGCONTEXTTRANSLATECTM (context, window.center.x, window.center.y);        CGCONTEXTCONCATCTM (context, window.transform); CGCONTEXTTRANSLATECTM (Context,-window.bounds.size.width * window.layer.anchorpoint.x,-window.bounds.size.height *                WINDOW.LAYER.ANCHORPOINT.Y); Correct for the orientation if (!ignoreorientation) {if (orientation = = Uiinterfaceorien                Tationlandscapeleft) {CGCONTEXTROTATECTM (context, (cgfloat) m_pi_2);            CGCONTEXTTRANSLATECTM (context, 0,-imagesize.width); } else if (orientation = = Uiinterfaceorientationlandscaperight) {CGCONTEXTROTATECTM (c                Ontext, (cgfloat)-m_pi_2);            CGCONTEXTTRANSLATECTM (context,-imagesize.height, 0); } else if (orientation = = Uiinterfaceorientationportraitupsidedown) {Cgcontextrotatec                TM (context, (cgfloat) M_PI);            CGCONTEXTTRANSLATECTM (context,-imagesize.width,-imagesize.height);       } } if ([Window Respondstoselector: @selector (drawviewhierarchyinrect:afterscreenupdates:)]) [Windo        W DrawViewHierarchyInRect:window.bounds Afterscreenupdates:no];                else [Window.layer Renderincontext:uigraphicsgetcurrentcontext ()];    Cgcontextrestoregstate (context);        } UIImage *image = Uigraphicsgetimagefromcurrentimagecontext ();        Uigraphicsendimagecontext (); return image;

The above is full screen, the following changes some areas, the following code is UIWindow category

H file

#import <UIKit/UIKit.h> @interface UIWindow (Category)-(UIImage *) screenshot;-(UIImage *) Screenshotwithrect: ( CGRect) rect; @end

m file

#import "Uiwindow+category.h" @implementation UIWindow (Category)-(UIImage *) screenshot{return [self screenshotwithre Ct:self.bounds];} -(UIImage *) Screenshotwithrect: (cgrect) rect{//Source (under MIT License): HTTPS://GITHUB.COM/SHINYDEVELOPMENT/SDSCR Eenshotcapture/blob/master/sdscreenshotcapture/sdscreenshotcapture.m#l35 BOOL ignoreorientation = SYSTEM_VERSION_G        Reater_than_or_equal_to (@ "8.0");        uiinterfaceorientation orientation = [UIApplication sharedapplication].statusbarorientation;    Cgsize imageSize = Cgsizezero;    CGFloat width = rect.size.width, height = rect.size.height;    CGFloat x = rect.origin.x, y = rect.origin.y; ImageSize = Cgsizemake (width, height);//Uigraphicsbeginimagecontextwithoptions (ImageSize, NO, [UIScreen mainscre    En].scale); if (uiinterfaceorientationisportrait (orientation) | | ignoreorientation) {//imagesize = [UIScreen mainscreen].bo        Unds.size;        ImageSize = Cgsizemake (width, height); x = rect.origin.x, y = rect.origin.y; } else {//imagesize = Cgsizemake ([UIScreen mainscreen].bounds.size.height, [UIScreen mainscreen].bounds.size        . width);        ImageSize = Cgsizemake (height, width);    x = rect.origin.y, y = rect.origin.x;    } uigraphicsbeginimagecontextwithoptions (ImageSize, NO, [UIScreen Mainscreen].scale);    Cgcontextref context = Uigraphicsgetcurrentcontext ();    Cgcontextsavegstate (context);    CGCONTEXTTRANSLATECTM (context, self.center.x, self.center.y);    CGCONTEXTCONCATCTM (context, self.transform); CGCONTEXTTRANSLATECTM (Context,-self.bounds.size.width * self.layer.anchorpoint.x,-self.bounds.size.height *        SELF.LAYER.ANCHORPOINT.Y); Correct for the orientation if (!ignoreorientation) {if (orientation = = UIINTERFACEORIENTATIONLANDSC            Apeleft) {CGCONTEXTROTATECTM (context, (cgfloat) m_pi_2);            CGCONTEXTTRANSLATECTM (context, 0,-self.bounds.size.height); CGCONTEXTTRANSLATECTM (Context,-X, y); } else if (orientation = = Uiinterfaceorientationlandscaperight) {CGCONTEXTROTATECTM (context, CGF            loat)-m_pi_2);            CGCONTEXTTRANSLATECTM (context,-self.bounds.size.width, 0);        CGCONTEXTTRANSLATECTM (context, x,-y); } else if (orientation = = Uiinterfaceorientationportraitupsidedown) {CGCONTEXTROTATECTM (context, (CGFloat)            M_PI);            CGCONTEXTTRANSLATECTM (context,-self.bounds.size.height,-self.bounds.size.width);        CGCONTEXTTRANSLATECTM (context, x, y);        } else {CGCONTEXTTRANSLATECTM (context,-X,-y);    }} else {CGCONTEXTTRANSLATECTM (context,-X,-y);        }//[self layoutifneeded]; if ([Self respondstoselector: @selector (drawviewhierarchyinrect:afterscreenupdates:)]) [Self    DrawViewHierarchyInRect:self.bounds Afterscreenupdates:no]; else [Self.layer Renderincontext:uigraphicsgetcurrentcontext ()];    Cgcontextrestoregstate (context);    UIImage *image = Uigraphicsgetimagefromcurrentimagecontext ();        Uigraphicsendimagecontext (); return image; @end

After this code rotates, the cropped area is rotated relative to the upper-left corner and is generally not used for rotation


View

H file

@interface UIView (screenshot)-(UIImage *) screenshot;-(UIImage *) Screenshotwithrect: (cgrect) rect; @end

M file

@implementation UIView (screenshot)-(UIImage *) screenshot{return [self screenshotWithRect:self.bounds];} -(UIImage *) Screenshotwithrect: (cgrect) rect;    {uigraphicsbeginimagecontextwithoptions (rect.size, NO, [UIScreen Mainscreen].scale);    Cgcontextref context = Uigraphicsgetcurrentcontext ();    if (context = = NULL) {return nil;    } cgcontextsavegstate (context);        CGCONTEXTTRANSLATECTM (context,-rect.origin.x,-RECT.ORIGIN.Y);    [Self layoutifneeded]; if ([Self respondstoselector: @selector (drawviewhierarchyinrect:afterscreenupdates:)])} {[Self drawviewhierarchyi    NRect:self.bounds Afterscreenupdates:no];    } else {[Self.layer renderincontext:context];    } cgcontextrestoregstate (context);    UIImage *image = Uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext (); NSData *imagedata = uiimagejpegrepresentation (image, 1); Convert to jpeg//image = [UIImage imagewithdata:imagedata scale:[uiscrEen mainscreen].scale]; return image;



iOS screenshot, the basic method of view

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.