/
PhotoViewController.m
executable file
·128 lines (100 loc) · 4.67 KB
/
PhotoViewController.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#import "PhotoViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
@interface PhotoViewController ()
@end
@implementation PhotoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView
{
CGRect mainScreenFrame = [[UIScreen mainScreen] bounds];
// Yes, I know I'm a caveman for doing all this by hand
GPUImageView *primaryView = [[GPUImageView alloc] initWithFrame:mainScreenFrame];
primaryView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
filterSettingsSlider = [[UISlider alloc] initWithFrame:CGRectMake(25.0, mainScreenFrame.size.height - 50.0, mainScreenFrame.size.width - 50.0, 40.0)];
[filterSettingsSlider addTarget:self action:@selector(updateSliderValue:) forControlEvents:UIControlEventValueChanged];
filterSettingsSlider.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
filterSettingsSlider.minimumValue = 0.0;
filterSettingsSlider.maximumValue = 3.0;
filterSettingsSlider.value = 1.0;
[primaryView addSubview:filterSettingsSlider];
photoCaptureButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
photoCaptureButton.frame = CGRectMake(round(mainScreenFrame.size.width / 2.0 - 150.0 / 2.0), mainScreenFrame.size.height - 90.0, 150.0, 40.0);
[photoCaptureButton setTitle:@"Capture Photo" forState:UIControlStateNormal];
photoCaptureButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[photoCaptureButton addTarget:self action:@selector(takePhoto:) forControlEvents:UIControlEventTouchUpInside];
[photoCaptureButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
[primaryView addSubview:photoCaptureButton];
self.view = primaryView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
stillCamera = [[GPUImageStillCamera alloc] init];
// filter = [[GPUImageGammaFilter alloc] init];
filter = [[GPUImageSketchFilter alloc] init];
[(GPUImageSketchFilter *)filter setImageHeightFactor:640.0];
[(GPUImageSketchFilter *)filter setImageWidthFactor:480.0];
// filter = [[GPUImageSmoothToonFilter alloc] init];
// filter = [[GPUImageSepiaFilter alloc] init];
[filter prepareForImageCapture];
GPUImageRotationFilter *rotationFilter = [[GPUImageRotationFilter alloc] initWithRotation:kGPUImageRotateRight];
[stillCamera addTarget:rotationFilter];
[rotationFilter addTarget:filter];
GPUImageView *filterView = (GPUImageView *)self.view;
[filter addTarget:filterView];
[stillCamera startCameraCapture];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)updateSliderValue:(id)sender
{
// [(GPUImagePixellateFilter *)filter setFractionalWidthOfAPixel:[(UISlider *)sender value]];
// [(GPUImageGammaFilter *)filter setGamma:[(UISlider *)sender value]];
}
- (IBAction)takePhoto:(id)sender;
{
[photoCaptureButton setEnabled:NO];
[stillCamera capturePhotoProcessedUpToFilter:filter withCompletionHandler:^(UIImage *processedImage, NSError *error){
NSData *dataForPNGFile = UIImageJPEGRepresentation(processedImage, 0.8);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError *error2 = nil;
if (![dataForPNGFile writeToFile:[documentsDirectory stringByAppendingPathComponent:@"FilteredPhoto.jpg"] options:NSAtomicWrite error:&error2])
{
return;
}
// Save to assets library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
CGImageRef imageRef = [processedImage CGImage];
CGImageRetain(imageRef);
[library writeImageToSavedPhotosAlbum:imageRef metadata:nil completionBlock:^(NSURL *assetURL, NSError *error2)
{
if (error2) {
NSLog(@"ERROR: the image failed to be written");
}
else {
NSLog(@"PHOTO SAVED - assetURL: %@", assetURL);
}
[processedImage self];
CGImageRelease(imageRef);
runOnMainQueueWithoutDeadlocking(^{
[photoCaptureButton setEnabled:YES];
});
}];
}];
}
@end