forked from x2on/PhotoViewer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EGODefaultActionViewController.m
174 lines (112 loc) · 4.57 KB
/
EGODefaultActionViewController.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
//
// EGODefaultActionViewController.m
// EGOPhotoViewer_Demo
//
// Created by Franklin Webber on 4/22/12.
// Copyright (c) 2012 Franklin Webber. All rights reserved.
//
#import "EGODefaultActionViewController.h"
@interface EGODefaultActionViewController ()
@property (nonatomic,strong) UIActionSheet *actionSheet;
@property (nonatomic,assign) BOOL _popover;
- (id<EGOPhoto>)photo;
- (UIActionSheet *)createActionSheet;
- (void)savePhoto;
- (void)copyPhoto;
- (void)emailPhoto;
@end
@implementation EGODefaultActionViewController
@synthesize photoSource = photoSource_;
@synthesize currentIndex = currentIndex_;
@synthesize actionSheet = actionSheet_;
@synthesize _popover;
- (void)viewDidLoad {
[super viewDidLoad];
if (self.actionSheet == nil) {
self.actionSheet = [self createActionSheet];
}
self.view.userInteractionEnabled = NO;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.actionSheet.title = [[self.photoSource photoAtIndex:self.currentIndex] title];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.actionSheet showInView:self.view];
}
#pragma mark - Photo Helper
- (id<EGOPhoto>)photo {
return [self.photoSource photoAtIndex:self.currentIndex];
}
#pragma mark - Create ActionSheet
- (UIActionSheet *)createActionSheet {
NSString *cancelButtonTitle = @"Cancel";
// TODO: the _popover is not cool zeus
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && !_popover) {
cancelButtonTitle = nil;
}
UIActionSheet *actionSheet = nil;
if ([MFMailComposeViewController canSendMail]) {
actionSheet = [[UIActionSheet alloc] initWithTitle:@"Actions"
delegate:self
cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:nil
otherButtonTitles:@"Save",@"Copy",@"Email", nil];
} else {
actionSheet = [[UIActionSheet alloc] initWithTitle:@"Actions"
delegate:self
cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:nil
otherButtonTitles:@"Save",@"Copy", nil];
}
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
return actionSheet;
}
#pragma mark - UIActionSheetDelegate Adherence
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == actionSheet.firstOtherButtonIndex) {
[self savePhoto];
} else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1) {
[self copyPhoto];
} else if (buttonIndex == actionSheet.firstOtherButtonIndex + 2) {
[self emailPhoto];
}
[self.view removeFromSuperview];
[self removeFromParentViewController];
}
#pragma mark - Actions
- (void)savePhoto {
UIImageWriteToSavedPhotosAlbum(self.photo.image,nil,nil,nil);
}
- (void)copyPhoto {
[[UIPasteboard generalPasteboard] setData:UIImagePNGRepresentation(self.photo.image) forPasteboardType:[self.photo.URL lastPathComponent]];
}
- (void)emailPhoto {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
[mailViewController setSubject:self.photo.title];
[mailViewController addAttachmentData:[NSData dataWithData:UIImagePNGRepresentation(self.photo.image)]
mimeType:@"image/png"
fileName:[self.photo.URL lastPathComponent]];
mailViewController.mailComposeDelegate = self;
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
mailViewController.modalPresentationStyle = UIModalPresentationPageSheet;
}
[self presentModalViewController:mailViewController animated:YES];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
[controller dismissModalViewControllerAnimated:YES];
NSString *mailError = nil;
switch (result) {
case MFMailComposeResultSent: ; break;
case MFMailComposeResultFailed: mailError = @"Failed sending media, please try again...";
break;
default:
break;
}
if (mailError != nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:mailError delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
@end