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