/
ViewController.m
90 lines (69 loc) · 3.31 KB
/
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#import "ViewController.h"
#import "QuartzCore/QuartzCore.h"
#import "AppDelegate.h"
@implementation ViewController
@synthesize rectLayer = _rectLayer;
- (void)viewDidLoad{
[super viewDidLoad];
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
self.view.backgroundColor = [UIColor grayColor];
self.rectLayer = [self drawHexagon:CGPointMake(200, 300) semiWidth:50 semiHeight:80];
[self.view.layer addSublayer:self.rectLayer];
[self captureScreenImageToDirectory:self.view dir:@"/Users/cat/try" file:@"myimage.png"];
}
// searchkey: capture screen shot screenshot save image
-(void)captureScreenImageToDirectory:(UIView*)myView dir:(NSString*)dir file:(NSString*)fileName{
NSString* fullPath = [dir stringByAppendingPathComponent:fileName];
NSLog(@"fullPath=%@", fullPath);
NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray* path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [path objectAtIndex:0];
NSLog(@"documentsDirectory=%@", documentsDirectory);
NSString* screenShotImageFile = [documentsDirectory stringByAppendingPathComponent:@"image.png"];
UIImage* outputImage = [self imageFromLayer:myView.layer];
[UIImagePNGRepresentation(outputImage) writeToFile:screenShotImageFile atomically:YES];
if([fileManager fileExistsAtPath:screenShotImageFile] == YES){
NSError* error;
[fileManager copyItemAtPath:screenShotImageFile toPath:fullPath error:&error];
}
NSLog(@"copy file from:[%@]", screenShotImageFile);
NSLog(@"to [%@]", fullPath);
}
// save layer to UIImage
- (UIImage *)imageFromLayer:(CALayer *)layer{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
//UIGraphicsBeginImageContextWithOptions([layer frame].size, NO, [UIScreen mainScreen].scale);
UIGraphicsBeginImageContextWithOptions(layer.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext([layer frame].size);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
-(CAShapeLayer*)drawHexagon:(CGPoint) location semiWidth:(CGFloat) semiWidth semiHeight:(CGFloat)semiHeight{
CAShapeLayer* shapeLayer = [CAShapeLayer layer];
UIBezierPath* path = [UIBezierPath bezierPath];
[path setLineJoinStyle:kCGLineJoinMiter];
CGFloat radians = 100.0;
NSInteger num = 6;
CGFloat interval = 2*M_PI/num;
CGFloat initX = radians*cosf(0*interval);
CGFloat initY = radians*sinf(0*interval);
[path moveToPoint:CGPointMake(location.x - semiWidth + initX, location.y - semiHeight + initY)];
for(int i=1; i<num; i++){
CGFloat x = radians*cosf(i*interval);
CGFloat y = radians*sinf(i*interval);
[path addLineToPoint:CGPointMake(location.x - semiWidth + x, location.y - semiHeight + y)];
}
[path closePath];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [[UIColor yellowColor] CGColor];
shapeLayer.fillColor = [[UIColor brownColor] CGColor];
shapeLayer.lineWidth = 4.0f;
return shapeLayer;
}
- (void)viewDidUnload{
[super viewDidUnload];
}
@end