Lossless screenshot of UIView

Source: Internet
Author: User

Non-destructive UIView

Description

1. The code of the Bad street

2. After the category is written, it is convenient to get the picture drawn directly from the drawrect.

3. You can directly draw a picture for the button to set the background image with

4. Lossless (including the alpha channel value is also lossless)

Source

////uiview+screensshot.h//Colorfulview////Created by youxianming on 15/7/17.//Copyright (c) 2015 youxianming. All rights reserved.//#import<UIKit/UIKit.h>@interfaceUIView (screensshot)/** * Lossless * * This function is called from any thread of your app. * * @return return the resulting picture*/-(UIImage *) screenshot;@end
////UIVIEW+SCREENSSHOT.M//Colorfulview////Created by youxianming on 15/7/17.//Copyright (c) 2015 youxianming. All rights reserved.//#import "uiview+screensshot.h"#import<objc/runtime.h>@implementationUIView (screensshot)-(UIImage *) screenshot {if(Self && self.frame.size.height &&self.frame.size.width) {uigraphicsbeginimagecontextwithoptions (self.frame.size, NO,0);        [Self.layer Renderincontext:uigraphicsgetcurrentcontext ()]; UIImage*image =Uigraphicsgetimagefromcurrentimagecontext ();                Uigraphicsendimagecontext (); returnimage; } Else {            returnNil; }}@end
////VIEWCONTROLLER.M//Colorfulview////Created by youxianming on 15/7/10.//Copyright (c) 2015 youxianming. All rights reserved.//#import "ViewController.h"#import "uiview+screensshot.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Blackcolor]; UIView*cyanview = [[UIView alloc] Initwithframe:cgrectmake ( -, -, -, -)]; Cyanview.backgroundcolor=[Uicolor Cyancolor]; Cyanview.alpha=0.5f;    [Self.view Addsubview:cyanview]; Uiimageview*imageview =[[Uiimageview alloc] Initwithimage:[cyanview screenshot]]; Imageview.frame= CGRectMake ( the, the, -, -); [Self.view Addsubview:imageview];}@end

Non-destructive UIView

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.