-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAQPhotoPickerView.m
96 lines (65 loc) · 2.95 KB
/
AQPhotoPickerView.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
//
// AQPhotoPickerVC.m
// MeeFree.com
//
// Created by Abdul_Qavi on 14/09/2014.
// Copyright (c) 2014 MeeFree.com. All rights reserved.
//
#import "AQPhotoPickerView.h"
@interface AQPhotoPickerViewOwner : NSObject
@property (nonatomic, weak) IBOutlet AQPhotoPickerView *decoupledView;
@end
@implementation AQPhotoPickerViewOwner
@end
@interface AQPhotoPickerView ()
@property (nonatomic, weak) UIViewController <AQPhotoPickerViewDelegate> *delegateViewController;
@property (nonatomic) UIImagePickerController *imagePickerController;
@end
@implementation AQPhotoPickerView
+(void)presentInViewController:(UIViewController<AQPhotoPickerViewDelegate>*) viewController
{
// Instantiating encapsulated here.
AQPhotoPickerViewOwner *owner = [AQPhotoPickerViewOwner new];
[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:owner options:nil];
// Pass in a reference of the viewController.
owner.decoupledView.delegateViewController = viewController;
[viewController.view addSubview:owner.decoupledView];
}
#pragma mark - Image capture and picker methods
- (IBAction)selectPhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.delegateViewController presentViewController:picker animated:YES completion:nil];
}
- (IBAction)takePhoto:(id)sender {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Test on real device, camera is not available in simulator" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
return;
}
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.delegateViewController presentViewController:picker animated:YES completion:nil];
[self.imagePickerController takePicture];
}
#pragma mark - UIImagePickerControllerDelegate
// This method is called when an image has been chosen from the library or taken from the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
[self removeFromSuperview];
[self.delegateViewController photoFromImagePickerView:chosenImage];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
[self removeFromSuperview];
}
#pragma mark - Utility methods
- (IBAction)backgroundViewTapped:(id)sender {
[self removeFromSuperview];
}
@end