-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathSNPictureViewController.m
185 lines (160 loc) · 8.42 KB
/
SNPictureViewController.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
#import "SNPictureViewController.h"
#ifndef SMSNinjaDebug
#define SETTINGS @"/var/mobile/Library/SMSNinja/smsninja.plist"
#define DATABASE @"/var/mobile/Library/SMSNinja/smsninja.db"
#define PICTURES @"/var/mobile/Library/SMSNinja/Pictures/"
#define PRIVATEPICTURES @"/var/mobile/Library/SMSNinja/PrivatePictures/"
#else
#define SETTINGS @"/Users/snakeninny/Library/Application Support/iPhone Simulator/7.0.3/Applications/0C9D35FB-B626-42B7-AAE9-45F6F537890B/Documents/var/mobile/Library/SMSNinja/smsninja.plist"
#define DATABASE @"/Users/snakeninny/Library/Application Support/iPhone Simulator/7.0.3/Applications/0C9D35FB-B626-42B7-AAE9-45F6F537890B/Documents/var/mobile/Library/SMSNinja/smsninja.db"
#define PICTURES @"/Users/snakeninny/Library/Application Support/iPhone Simulator/7.0.3/Applications/0C9D35FB-B626-42B7-AAE9-45F6F537890B/Documents/var/mobile/Library/SMSNinja/Pictures/"
#define PRIVATEPICTURES @"/Users/snakeninny/Library/Application Support/iPhone Simulator/7.0.3/Applications/0C9D35FB-B626-42B7-AAE9-45F6F537890B/Documents/var/mobile/Library/SMSNinja/PrivatePictures/"
#endif
@implementation SNPictureViewController
@synthesize idString;
@synthesize flag;
- (void)dealloc
{
[idString release];
idString = nil;
[flag release];
flag = nil;
[pictureScrollView release];
pictureScrollView = nil;
[super dealloc];
}
- (instancetype)init
{
if (self = [super init])
{
pictureScrollView = [[UIScrollView alloc] init];
pictureScrollView.delegate = self;
pictureScrollView.frame = CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
pictureScrollView.contentSize = CGSizeZero;
pictureScrollView.showsVerticalScrollIndicator = NO;
pictureScrollView.showsHorizontalScrollIndicator = NO;
pictureScrollView.pagingEnabled = YES;
pictureScrollView.userInteractionEnabled = YES;
pictureScrollView.backgroundColor = [UIColor whiteColor];
self.wantsFullScreenLayout = YES;
if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) self.automaticallyAdjustsScrollViewInsets = NO;
[self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save", @"Save") style:UIBarButtonItemStyleBordered target:self action:@selector(saveToAlbum)] autorelease]];
}
return self;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_7_0)
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
pictureScrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * picturesCount, pictureScrollView.bounds.size.height);
if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_7_0)
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
}
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[pictureScrollView addGestureRecognizer:tapGesture];
[tapGesture setNumberOfTapsRequired:1];
[tapGesture release];
for (int i = 0; i < 2; i++)
{
NSString *fileName = nil;
if ([self.flag isEqualToString:@"black"]) fileName = [NSString stringWithFormat:@"%@%@-%d.%@", PICTURES, self.idString, i, @"png"];
else if ([self.flag isEqualToString:@"private"]) fileName = [NSString stringWithFormat:@"%@%@-%d.%@", PRIVATEPICTURES, self.idString, i, @"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileName];
UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * i, 0.0f, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = image;
imageView.tag = i + 1;
[pictureScrollView addSubview:imageView];
[image release];
[imageView release];
}
self.title = [NSString stringWithFormat:NSLocalizedString(@"Picture %d/%d", @"Picture %d/%d"), 1, picturesCount];
[self.view addSubview:pictureScrollView];
}
- (void)restoreTitle:(NSString *)title
{
if ([self.title isEqualToString:NSLocalizedString(@"Done saving", @"Done saving")]) self.title = title;
}
- (void)saveToAlbum
{
int currentViewIndex = ceil(pictureScrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
NSString *fileName = nil;
if ([self.flag isEqualToString:@"black"]) fileName = [NSString stringWithFormat:@"%@%@-%d.%@", PICTURES, self.idString, currentViewIndex, @"png"];
else if ([self.flag isEqualToString:@"private"]) fileName = [NSString stringWithFormat:@"%@%@-%d.%@", PRIVATEPICTURES, self.idString, currentViewIndex, @"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileName];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[image release];
NSString *originalTitle = self.title;
self.title = NSLocalizedString(@"Done saving", @"Done saving");
[self performSelector:@selector(restoreTitle:) withObject:originalTitle afterDelay:2.0f];
}
- (void)tap:(UITapGestureRecognizer *)gesture
{
__block BOOL shouldHide = !self.navigationController.navigationBarHidden;
SNPictureViewController *weakSelf = self;
if (!shouldHide)
{
[[UIApplication sharedApplication] setStatusBarHidden:shouldHide withAnimation:UIStatusBarAnimationFade];
[self.navigationController setNavigationBarHidden:shouldHide animated:NO];
}
[UIView transitionWithView:self.navigationController.view
duration:0.25f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[[UIApplication sharedApplication] setStatusBarHidden:shouldHide withAnimation:UIStatusBarAnimationFade];
[weakSelf.navigationController setNavigationBarHidden:shouldHide animated:NO];
}
completion:nil];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)view
{
int currentViewIndex = ceil(view.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
self.title = [NSString stringWithFormat:NSLocalizedString(@"Picture %d/%d", @"Picture %d/%d"), currentViewIndex + 1, picturesCount];
}
- (void)scrollViewDidScroll:(UIScrollView *)view
{
int currentViewIndex = ceil(view.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
int currentViewTag = currentViewIndex + 1;
if ([view viewWithTag:currentViewTag + 1] == nil && currentViewTag != picturesCount) // next
{
NSString *fileName = nil;
if ([self.flag isEqualToString:@"black"]) fileName = [NSString stringWithFormat:@"%@%@-%d.%@", PICTURES, self.idString, currentViewIndex + 1, @"png"];
else if ([self.flag isEqualToString:@"private"]) fileName = [NSString stringWithFormat:@"%@%@-%d.%@", PRIVATEPICTURES, self.idString, currentViewIndex + 1, @"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileName];
UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * (currentViewIndex + 1), 0.0f, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = image;
imageView.tag = currentViewTag + 1;
[pictureScrollView addSubview:imageView];
[image release];
[imageView release];
}
if ([view viewWithTag:currentViewTag - 1] == nil && currentViewTag != 0) // previous
{
NSString *fileName = nil;
if ([self.flag isEqualToString:@"black"]) fileName = [NSString stringWithFormat:@"%@%@-%d.%@", PICTURES, self.idString, currentViewIndex - 1, @"png"];
else if ([self.flag isEqualToString:@"private"]) fileName = [NSString stringWithFormat:@"%@%@-%d.%@", PRIVATEPICTURES, self.idString, currentViewIndex - 1, @"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileName];
UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * (currentViewIndex - 1), 0.0f, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = image;
imageView.tag = currentViewTag - 1;
[pictureScrollView addSubview:imageView];
[image release];
[imageView release];
}
for (UIView *subview in [view subviews])
if (subview.tag != currentViewTag && subview.tag != currentViewTag - 1 && subview.tag != currentViewTag + 1)
[subview removeFromSuperview];
}
@end