-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUIMediaAlertController.m
345 lines (283 loc) · 12.4 KB
/
UIMediaAlertController.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//
// UIMediaAlertController.m
// UIMediaAlertController
//
// Created by R3V0 on 25/05/2018.
// Copyright © 2018 iosdec. All rights reserved.
//
#import "UIMediaAlertController.h"
@implementation UIMediaAlertController
__strong UIMediaAlertController *sharedController;
__strong UIMedia *sharedMedia;
+ (void)presentWithType:(MediaType)type picked:(void (^)(void))picked {
if (!sharedController) {
sharedController = [[UIMediaAlertController alloc] init];
} if (!sharedMedia) {
sharedMedia = [[UIMedia alloc] init];
}
[sharedController setController:[sharedController findTopViewController]];
[sharedController setType:type];
[sharedController setPicked:picked];
[sharedController setRemoved:nil];
[sharedController removeObjects];
if (!sharedController.media) {
sharedController.media = sharedMedia;
}
[sharedController showAlertController_pre:sharedController];
}
+ (void)presentWithType:(MediaType)type picked:(void (^)(void))picked removed:(void (^)(void))removed {
if (!sharedController) {
sharedController = [[UIMediaAlertController alloc] init];
} if (!sharedMedia) {
sharedMedia = [[UIMedia alloc] init];
}
[sharedController setController:[sharedController findTopViewController]];
[sharedController setType:type];
[sharedController setPicked:picked];
[sharedController setRemoved:removed];
[sharedController removeObjects];
if (!sharedController.media) {
sharedController.media = sharedMedia;
}
[sharedController showAlertController_pre:sharedController];
}
+ (void)resetMedia {
sharedMedia.image = nil;
sharedMedia.imageURL = nil;
sharedMedia.videoURL = nil;
sharedMedia.videoThumb = nil;
}
+ (UIMedia *)media {
if (sharedMedia) { return sharedMedia; }
else { sharedMedia = [[UIMedia alloc] init]; return sharedMedia; }
}
- (void)presentWithType:(MediaType)type picked:(void (^)(void))picked {
[self setController:[self findTopViewController]];
[self setType:type];
[self setPicked:picked];
[self setRemoved:nil];
[self removeObjects];
if (!self.media) {
self.media = [[UIMedia alloc] init];
}
[self showAlertController_pre:self];
}
- (void)presentWithType:(MediaType)type picked:(void (^)(void))picked removed:(void (^)(void))removed {
[self setController:[self findTopViewController]];
[self setType:type];
[self setPicked:picked];
[self setRemoved:removed];
[self removeObjects];
if (!self.media) {
self.media = [[UIMedia alloc] init];
}
[self showAlertController_pre:self];
}
- (void)resetMedia {
self.media.image = nil;
self.media.imageURL = nil;
self.media.videoURL = nil;
self.media.videoThumb = nil;
}
#pragma mark - Present Preperation:
- (void)showAlertController_pre:(UIMediaAlertController *)instance {
if (self.delay) {
[instance performSelector:@selector(showAlertController) withObject:nil afterDelay:self.delay];
return;
} else {
[self showAlertController];
}
}
- (void)showAlertController {
// check if type is image:
if (self.type == MediaTypeImage) {
self.title = @"Add Image";
self.message = @"\n\nNo Image\n\n";
if (self.media.image) {
self.title = @"Image Options";
self.message = @"";
NSUInteger lines = round(kUIM_MediaHeight/kUIM_LineSpacing);
for (int i = 0; i < lines; i++) {
self.message = [self.message stringByAppendingString:@"\n"];
}
}
self.imageView = [[UIImageView alloc] init];
self.imageView.frame = CGRectMake(0, 62, kUIM_MediaWidth, kUIM_MediaHeight);
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.center = CGPointMake(self.controller.view.center.x-10, self.imageView.center.y);
self.imageView.image = self.media.image;
self.imageView.layer.cornerRadius = 5;
self.imageView.clipsToBounds = YES;
}
// check if type is video:
if (self.type == MediaTypeVideo) {
self.title = @"Add Video";
self.message = @"\n\nNo Video\n\n";
if (self.media.videoURL) {
self.title = @"Video Options";
self.message = @"";
NSUInteger lines = round(kUIM_MediaHeight/kUIM_LineSpacing);
for (int i = 0; i < lines; i++) {
self.message = [self.message stringByAppendingString:@"\n"];
}
}
self.videoView = [[UIView alloc] init];
self.videoView.frame = CGRectMake(0, 62, kUIM_MediaWidth, kUIM_MediaHeight);
self.videoView.center = CGPointMake(self.controller.view.center.x-10, self.videoView.center.y);
self.videoView.layer.cornerRadius = 5;
self.videoView.clipsToBounds = YES;
AVPlayer *player = [[AVPlayer alloc] init];
if (self.media.videoURL) { player = [[AVPlayer alloc] initWithURL:self.media.videoURL]; }
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
layer.frame = CGRectMake(0, 0, self.videoView.frame.size.width, self.videoView.frame.size.height);
[self.videoView.layer insertSublayer:layer atIndex:0];
self.playerRef = player;
}
//
// ** present UIAlertController:
//
UIAlertController *ac = [UIAlertController alertControllerWithTitle:self.title message:self.message preferredStyle:UIAlertControllerStyleActionSheet];
// check if the image isn't nil.. if so add it:
if (self.media.image) {
[ac.view addSubview:self.imageView];
}
// check if the media url isn't nil.. if so add view:
if (self.media.videoURL) {
[ac.view addSubview:self.videoView];
}
// create actions:
UIAlertAction *add = [UIAlertAction actionWithTitle:[NSString stringWithFormat:@"Add %@",[self typeString]] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self presentPickerWithType:self.type];
}];
if (self.type == MediaTypeImage) {
if (!self.media.image) {
[ac addAction:add];
}
}
if (self.type == MediaTypeVideo) {
if (!self.media.videoURL) {
[ac addAction:add];
}
}
UIAlertAction *replace = [UIAlertAction actionWithTitle:[NSString stringWithFormat:@"Replace %@",[self typeString]] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (self.type == MediaTypeImage) { self.media.image = nil; }
if (self.type == MediaTypeVideo) { self.media.videoURL = nil; }
[self presentPickerWithType:self.type];
}];
if (self.type == MediaTypeImage) {
if (self.media.image) {
[ac addAction:replace];
}
}
if (self.type == MediaTypeVideo) {
if (self.media.videoURL) {
[ac addAction:replace];
}
}
UIAlertAction *removeImage = [UIAlertAction actionWithTitle:[NSString stringWithFormat:@"Remove %@",[self typeString]] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (self.type == MediaTypeImage) { self.media.image = nil; }
if (self.type == MediaTypeVideo) { self.media.videoURL = nil; }
if (self.removed) {
self.removed();
[self presentWithType:self.type picked:self.picked removed:self.removed];
} else {
[self presentWithType:self.type picked:self.picked removed:nil];
}
}];
if (self.type == MediaTypeImage) {
if (self.media.image) {
[ac addAction:removeImage];
}
}
if (self.type == MediaTypeVideo) {
if (self.media.videoURL) {
[ac addAction:removeImage];
}
}
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
if (self.playerRef) {
[self.playerRef seekToTime:CMTimeMake(0, 1)];
[self.playerRef pause];
}
}];
[ac addAction:cancel];
// add popover for iPad
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[ac setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [ac popoverPresentationController];
popPresenter.sourceView = self.controller.view;
popPresenter.sourceRect = CGRectMake(self.controller.view.frame.size.width/2, 200, 2, 2);
}
// present alert controller:
[self.controller presentViewController:ac animated:YES completion:^{
if (self.media.videoURL) {
if (self.playerRef) {
[self.playerRef play];
}
}
}];
}
#pragma mark - Present:
- (void)presentPickerWithType:(MediaType)type {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
if (type == MediaTypeImage) { picker.mediaTypes = @[(NSString *)kUTTypeImage]; }
if (type == MediaTypeVideo) { picker.mediaTypes = @[(NSString *)kUTTypeMovie]; }
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsEditing = YES;
[self.controller presentViewController:picker animated:YES completion:nil];
}
#pragma mark - UIImagePickerController Delegate:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
if (self.type == MediaTypeImage) {
UIImage *image = info[UIImagePickerControllerEditedImage];
self.media.image = image;
self.picked();
}
if (self.type == MediaTypeVideo) {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
NSURL *videoURL = (NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
self.media.videoThumb = thumb;
self.media.videoURL = videoURL;
self.picked();
}
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Extras:
- (UIViewController *)findTopViewController {
UIViewController *rootController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (rootController.presentedViewController) {
rootController = rootController.presentedViewController;
} return rootController;
}
- (NSString *)typeString {
if (self.type == MediaTypeImage) { return @"Image"; }
else if (self.type == MediaTypeVideo) { return @"Video"; }
else { return @"Media"; }
}
- (void)removeObjects {
if (self.imageView) {
[self.imageView removeFromSuperview];
self.imageView = nil;
} if (self.videoView) {
[self.videoView removeFromSuperview];
self.videoView = nil;
} if (self.playerRef) {
self.playerRef = nil;
}
}
@end