-
Notifications
You must be signed in to change notification settings - Fork 38
/
EIFullDemoViewController.m
250 lines (193 loc) · 12.1 KB
/
EIFullDemoViewController.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
File: EIDemoViewController.m
Abstract: Demo Controller to show usage of image picker with blocks,
and subsequent resizing, saving, clipping and masking of returned image
Copyright (c) 2012 Dillion Tan
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#import "EIFullDemoViewController.h"
@interface EIFullDemoViewController ()
@end
@implementation EIFullDemoViewController
@synthesize summaryLabel;
@synthesize buttonWithImage;
@synthesize buttonWithBackgroundImage;
@synthesize imageViewWithRoundedCorners;
@synthesize layerMaskedCircleImageView;
@synthesize imageMaskedRadialGradientImageView;
@synthesize imagePickerDelegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// show how a button looks with returned image set as button image without resizing
self.buttonWithImage = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 120, 120)];
buttonWithImage.layer.cornerRadius = 20.0f;
buttonWithImage.layer.borderColor = [UIColor blueColor].CGColor;
buttonWithImage.layer.borderWidth = 1.0f;
[buttonWithImage setImage:[UIImage imageNamed:@"Icon.png"] forState:UIControlStateNormal];
[buttonWithImage addTarget:self action:@selector(presentPhotoPicker) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonWithImage];
// show how a button looks with returned image set as background button image without resizing
self.buttonWithBackgroundImage = [[UIButton alloc] initWithFrame:CGRectMake(160, 10, 120, 120)];
buttonWithBackgroundImage.layer.cornerRadius = 20.0f;
buttonWithBackgroundImage.layer.borderColor = [UIColor blueColor].CGColor;
buttonWithBackgroundImage.layer.borderWidth = 1.0f;
[buttonWithBackgroundImage setBackgroundImage:[UIImage imageNamed:@"Icon.png"] forState:UIControlStateNormal];
[buttonWithBackgroundImage addTarget:self action:@selector(presentPhotoPicker) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonWithBackgroundImage];
self.summaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 140, 280, 30)];
summaryLabel.font = [summaryLabel.font fontWithSize:11];
summaryLabel.numberOfLines = 0;
summaryLabel.text = @"Select one of the 2 buttons above to add an image from the picker.";
[self.view addSubview:summaryLabel];
// displaying the image in a rounded rect
UIImageView *backingViewForRoundedCorner = [[UIImageView alloc] initWithFrame:CGRectMake(20, 180, 120, 120)];
backingViewForRoundedCorner.layer.cornerRadius = 20.0f;
backingViewForRoundedCorner.clipsToBounds = YES;
[self.view addSubview:backingViewForRoundedCorner];
self.imageViewWithRoundedCorners = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
imageViewWithRoundedCorners.backgroundColor = [UIColor lightGrayColor];
[backingViewForRoundedCorner addSubview:imageViewWithRoundedCorners];
// displaying the image in a circle by using a shape layer
// layer fill color controls the masking
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.backgroundColor = [UIColor whiteColor].CGColor;
UIBezierPath *layerPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(1, 1, 118, 118)];
maskLayer.path = layerPath.CGPath;
maskLayer.fillColor = [UIColor blackColor].CGColor;
// use another view for clipping so that when the image size changes, the masking layer does not need to be repositioned
UIView *clippingViewForLayerMask = [[UIView alloc] initWithFrame:CGRectMake(160, 180, 120, 120)];
clippingViewForLayerMask.layer.mask = maskLayer;
clippingViewForLayerMask.clipsToBounds = YES;
[self.view addSubview:clippingViewForLayerMask];
self.layerMaskedCircleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
layerMaskedCircleImageView.backgroundColor = [UIColor lightGrayColor];
[clippingViewForLayerMask addSubview:layerMaskedCircleImageView];
// displaying the image in a radial gradient by using a png mask
// image alpha channel controls the masking
CALayer *imageMaskLayer = [CALayer layer];
UIImage *pattern = [UIImage imageNamed:@"pattern.png"];
imageMaskLayer.contents = (__bridge id)pattern.CGImage;
imageMaskLayer.frame = CGRectMake(0, 0, pattern.size.width, pattern.size.height);
// use another view for clipping so that when the image size changes, the masking layer does not need to be repositioned
UIView *clippingViewForLayerImageMask = [[UIView alloc] initWithFrame:CGRectMake(20, 310, 120, 120)];
clippingViewForLayerImageMask.layer.mask = imageMaskLayer;
clippingViewForLayerImageMask.clipsToBounds = YES;
[self.view addSubview:clippingViewForLayerImageMask];
self.imageMaskedRadialGradientImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
imageMaskedRadialGradientImageView.backgroundColor = [UIColor lightGrayColor];
[clippingViewForLayerImageMask addSubview:imageMaskedRadialGradientImageView];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"savedImage"]) { // test cached image
NSString *urlString = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedImage"];
UIImage *cachedImage = [NSData imageFromFile:[[[NSFileManager defaultManager] cacheDataPath] stringByAppendingPathComponent:urlString]];
if (cachedImage) {
NSLog(@"logical size is %f:%f scale %f", cachedImage.size.width, cachedImage.size.height, cachedImage.scale);
[self setOriginalImage:[UIImage imageNamed:@"Icon.png"] resizedImage:cachedImage];
}
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)presentPhotoPicker
{
if (!imagePickerDelegate) {
self.imagePickerDelegate = [[EIImagePickerDelegate alloc] init];
__weak EIFullDemoViewController *weakController = self;
[imagePickerDelegate setImagePickerCompletionBlock:^(UIImage *pickerImage) {
__strong EIFullDemoViewController *strongController = weakController;
NSLog(@"logical size is %f:%f scale %f", pickerImage.size.width, pickerImage.size.height, pickerImage.scale);
// Use display size to constrain resizing
// 120 is the logical display size
UIImage *resizedImage = [pickerImage resizedImageWithContentMode:UIViewContentModeScaleAspectFill bounds:CGSizeMake(120, 120) interpolationQuality:kCGInterpolationDefault];
// Use uncompressed size to constrain resizing
// UIImage *resizedImage = [pickerImage resizedImageWithUncompressedSizeInMB:1.0 interpolationQuality:kCGInterpolationDefault];
NSLog(@"logical size is %f:%f scale %f", resizedImage.size.width, resizedImage.size.height, resizedImage.scale);
[strongController setOriginalImage:pickerImage resizedImage:resizedImage];
// remove the image if previously saved
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"savedImage"]) {
NSString *cachedFilePath = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedImage"];
[[NSFileManager defaultManager] removeItemAtURL:[NSURL URLWithString:cachedFilePath] error:NULL];
}
NSString *assetName = [NSString stringWithFormat:@"%@.png", [[NSProcessInfo processInfo] globallyUniqueString]];
assetName = [assetName stringByAppendingScaleSuffix]; // add scale suffix to extension
NSString *assetPath = [[[NSFileManager defaultManager] cacheDataPath] stringByAppendingPathComponent:assetName];
// saving synchronously
// [UIImagePNGRepresentation(resizedImage) writeToFile:assetPath atomically:NO];
// [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@", assetName] forKey:@"savedImage"];
[[EIOperationManager defaultManager] saveImage:resizedImage toPath:assetPath withBlock:^(BOOL success) {
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@", assetName] forKey:@"savedImage"];
}];
}];
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Photo Library", @"Camera", nil];
[actionSheet showInView:self.view];
} else {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Photo Library", nil];
[actionSheet showInView:self.view];
}
}
- (void)setOriginalImage:(UIImage *)aImage resizedImage:(UIImage *)bImage
{
[buttonWithImage setImage:aImage forState:UIControlStateNormal];
[buttonWithBackgroundImage setBackgroundImage:aImage forState:UIControlStateNormal];
imageViewWithRoundedCorners.image = bImage;
imageViewWithRoundedCorners.frame = CGRectMake(0, 0, bImage.size.width, bImage.size.height);
imageViewWithRoundedCorners.center = CGPointMake(imageViewWithRoundedCorners.superview.frame.size.width/2, imageViewWithRoundedCorners.superview.frame.size.height/2);
layerMaskedCircleImageView.image = bImage;
layerMaskedCircleImageView.frame = CGRectMake(0, 0, bImage.size.width, bImage.size.height);
layerMaskedCircleImageView.center = CGPointMake(layerMaskedCircleImageView.superview.frame.size.width/2, layerMaskedCircleImageView.superview.frame.size.height/2);
imageMaskedRadialGradientImageView.image = bImage;
imageMaskedRadialGradientImageView.frame = CGRectMake(0, 0, bImage.size.width, bImage.size.height);
imageMaskedRadialGradientImageView.center = CGPointMake(layerMaskedCircleImageView.superview.frame.size.width/2, layerMaskedCircleImageView.superview.frame.size.height/2);
}
# pragma mark -
# pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
[imagePickerDelegate presentFromController:self withSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
break;
case 1:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[imagePickerDelegate presentFromController:self withSourceType:UIImagePickerControllerSourceTypeCamera];
}
break;
default:
break;
}
}
@end