1、绘制简单的形状
#import <UIKit/UIKit.h> @interface MyView : UIView @property (strong, nonatomic)UIImage *image; @end
1 #import "MyView.h" 2 3 @implementation MyView 4 5 - (id)initWithFrame:(CGRect)frame 6 { 7 self = [super initWithFrame:frame]; 8 if (self) { 9 // Initialization code 10 } 11 return self; 12 } 13 14 // Only override drawRect: if you perform custom drawing. 15 // An empty implementation adversely affects performance during animation. 16 - (void)drawRect:(CGRect)rect 17 { 18 CGContextRef context = UIGraphicsGetCurrentContext(); 19 // Draw rectangle 20 CGRect drawingRect = CGRectMake(0.0, 20.0f, 100.0f, 180.0f); 21 const CGFloat *rectColorComponents = CGColorGetComponents([[UIColor greenColor] CGColor]); 22 CGContextSetFillColor(context, rectColorComponents); 23 CGContextFillRect(context, drawingRect); 24 // Draw ellipse 25 CGRect ellipseRect = CGRectMake(140.0f, 200.0f, 75.0f, 50.0f); 26 const CGFloat *ellipseColorComponenets = CGColorGetComponents([[UIColor blueColor] CGColor]); 27 CGContextSetFillColor(context, ellipseColorComponenets); 28 CGContextFillEllipseInRect(context, ellipseRect); 29 // Draw parallelogram 30 CGContextBeginPath(context); 31 CGContextMoveToPoint(context, 0.0f, 0.0f); 32 CGContextAddLineToPoint(context, 100.0f, 0.0f); 33 CGContextAddLineToPoint(context, 140.0f, 100.0f); 34 CGContextAddLineToPoint(context, 40.0f, 100.0f); 35 CGContextClosePath(context); 36 CGContextSetGrayFillColor(context, 0.4f, 0.85f); 37 CGContextSetGrayStrokeColor(context, 0.0, 0.0); 38 CGContextFillPath(context); 39 40 if (self.image) 41 { 42 CGFloat imageWidth = self.frame.size.width / 2; 43 CGFloat imageHeight = self.frame.size.height / 2; 44 CGRect imageRect = CGRectMake(imageWidth, imageHeight, imageWidth, imageHeight); 45 [self.image drawInRect:imageRect]; 46 } 47 } 48 49 @end
2、屏幕截图编程
#import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> #import "MyView.h" @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet MyView *myView; @end
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad 10 { 11 [super viewDidLoad]; 12 // Do any additional setup after loading the view, typically from a nib. 13 } 14 15 - (void)didReceiveMemoryWarning 16 { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (BOOL) canBecomeFirstResponder 22 { 23 return YES; 24 } 25 26 - (void) viewWillAppear: (BOOL)animated 27 { 28 [self.view becomeFirstResponder]; 29 [super viewWillAppear:animated]; 30 } 31 32 - (void) viewWillDisappear: (BOOL)animated 33 { 34 [self.view resignFirstResponder]; 35 [super viewWillDisappear:animated]; 36 } 37 38 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 39 { 40 if (event.subtype == UIEventSubtypeMotionShake) 41 { 42 // Device was shaken 43 44 // Acquire image of current layer 45 UIGraphicsBeginImageContext(self.view.bounds.size); 46 CGContextRef context = UIGraphicsGetCurrentContext(); 47 [self.view.layer renderInContext:context]; 48 //截图方法 49 UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); 50 UIGraphicsEndImageContext(); 51 52 self.myView.image = image; 53 //UIView重新调用drawRect方法,从而包含最新的变化。 54 [self.myView setNeedsDisplay]; 55 56 } 57 } 58 59 @end