-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewPhotoVC.m
executable file
·154 lines (137 loc) · 6.63 KB
/
ViewPhotoVC.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
//
// ViewPhotoVC.m
// Calculator
//
// Created by Corey Allen Pett on 10/23/15.
// Copyright © 2015 Corey Allen Pett. All rights reserved.
//
#import <MessageUI/MessageUI.h>
#import "PhotoCVC.h"
#import "ViewPhotoVC.h"
@interface ViewPhotoVC () <UIScrollViewDelegate, MFMailComposeViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *displayImage;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end
@implementation ViewPhotoVC
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.delegate = self;
// Do any additional setup after loading the view.
NSData *imageData = [self.photoStorage.fullImageArray objectAtIndex:self.photoStorage.currentPhoto];
UIImage *fullResImage = [UIImage imageWithData:imageData];
self.displayImage.image = fullResImage;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self.navigationController setToolbarHidden:NO animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)doneButton:(id)sender
{
//Pass photo storage back to PhotoCVC
PhotoCVC *destinationController = [[PhotoCVC alloc] init];
destinationController.photoStorage = self.photoStorage;
[self dismissViewControllerAnimated:YES completion:nil];
}
//Previous photo
- (IBAction)backArrow:(id)sender
{
long length = self.photoStorage.fullImageArray.count - 1;
//If on the first photo, cycle current photo to the last
if(self.photoStorage.currentPhoto == 0){
self.photoStorage.currentPhoto = length;
NSData *imageData = [self.photoStorage.fullImageArray objectAtIndex:self.photoStorage.currentPhoto];
UIImage *fullResImage = [UIImage imageWithData:imageData];
self.displayImage.image = fullResImage;
NSLog(@"Back Arrow - current photo = %lu", self.photoStorage.currentPhoto);
}
else{
NSData *imageData = [self.photoStorage.fullImageArray objectAtIndex:self.photoStorage.currentPhoto--];
UIImage *fullResImage = [UIImage imageWithData:imageData];
self.displayImage.image = fullResImage;
NSLog(@"Back Arrow - current photo = %lu", self.photoStorage.currentPhoto);
}
}
//Next photo
- (IBAction)forwardArrow:(id)sender
{
long length = self.photoStorage.fullImageArray.count - 1;
//If on the last photo, cycle current photo to the first
if(self.photoStorage.currentPhoto == length){
self.photoStorage.currentPhoto = 0;
NSData *imageData = [self.photoStorage.fullImageArray objectAtIndex:self.photoStorage.currentPhoto];
UIImage *fullResImage = [UIImage imageWithData:imageData];
self.displayImage.image = fullResImage;
NSLog(@"Front Arrow - current photo = %lu", self.photoStorage.currentPhoto);
}
else{
NSData *imageData = [self.photoStorage.fullImageArray objectAtIndex:self.photoStorage.currentPhoto++];
UIImage *fullResImage = [UIImage imageWithData:imageData];
self.displayImage.image = fullResImage;
NSLog(@"Frontsd Arrow - current photo = %lu", self.photoStorage.currentPhoto);
}
}
//Fix and animate as well
- (IBAction)trashButton:(id)sender
{
long length = self.photoStorage.fullImageArray.count;
NSLog(@"current photo before removing = %lu", self.photoStorage.currentPhoto);
//If user deletes last photo, dismiss modal back to collectionView
if(self.photoStorage.currentPhoto == 0 && length == 1) {
[self.photoStorage deletePhoto];
[self.photoStorage.fullImageArray removeObjectAtIndex:self.photoStorage.currentPhoto];
[self.photoStorage.scaledImageArray removeObjectAtIndex:self.photoStorage.currentPhoto];
NSLog(@"current photo after removing = %lu", self.photoStorage.currentPhoto);
PhotoCVC *destinationController = [[PhotoCVC alloc] init];
destinationController.photoStorage = self.photoStorage;
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
//If user deletes first photo but theres still more photos, don't dismiss modal
else if ((self.photoStorage.currentPhoto == 0) && (length > 0)){
[self.photoStorage deletePhoto];
[self.photoStorage.fullImageArray removeObjectAtIndex:self.photoStorage.currentPhoto];
[self.photoStorage.scaledImageArray removeObjectAtIndex:self.photoStorage.currentPhoto];
NSLog(@"current photo after removing = %lu", self.photoStorage.currentPhoto);
NSData *imageData = [self.photoStorage.fullImageArray objectAtIndex:self.photoStorage.currentPhoto];
UIImage *fullResImage = [UIImage imageWithData:imageData];
self.displayImage.image = fullResImage;
}
//Delete selected photo and move current photo to the previous one
else {
[self.photoStorage deletePhoto];
[self.photoStorage.fullImageArray removeObjectAtIndex:self.photoStorage.currentPhoto];
[self.photoStorage.scaledImageArray removeObjectAtIndex:self.photoStorage.currentPhoto];
self.photoStorage.currentPhoto--;
NSLog(@"current photo after removing = %lu", self.photoStorage.currentPhoto);
NSData *imageData = [self.photoStorage.fullImageArray objectAtIndex:self.photoStorage.currentPhoto];
UIImage *fullResImage = [UIImage imageWithData:imageData];
self.displayImage.image = fullResImage;
}
}
//Action view
- (IBAction)actionButton:(id)sender
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *exportAction = [UIAlertAction actionWithTitle:@"Export Photo"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
NSData *imageData = [self.photoStorage.fullImageArray objectAtIndex:self.photoStorage.currentPhoto];
UIImage *fullResImage = [UIImage imageWithData:imageData];
UIImageWriteToSavedPhotosAlbum(fullResImage, nil, nil, nil);
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){}];
[alert addAction:exportAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.displayImage;
}
@end