-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tweak.xm
279 lines (192 loc) · 7.57 KB
/
Tweak.xm
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
#import <UIKit/UIPushButton.h>
#import <UIKit/UIAlert.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UIProgressHUD.h>
#import <substrate.h>
@interface MLPhoto
+(CGImageRef)createUnrotatedImageWithSize:(CGSize)size originalImage:(CGImageRef)image imageOrientation:(int)orientation;
-(id)pathForOriginalFile;
+(BOOL)instancesRespondToSelector:(SEL)selector;
@end
@interface PLAlbumsController : UIViewController
-(UIImage *)currentUIImage;
-(MLPhoto *)currentImage;
-(void)tweakWeekGrayScaleImageSaveCompleted;
-(id)currentImageView;
@end
@interface PLPhotoScrollerViewController : UIViewController
-(UIImage *)currentUIImage;
-(MLPhoto *)currentImage;
-(void)tweakWeekGrayScaleImageSaveCompleted;
-(id)currentImageView;
@end
@interface UIThreePartButton : UIPushButton
@end
@interface UIActionSheet (additions)
-(id)buttonAtIndex:(int)index;
-(void)setDefaultButton:(id)button;
-(id)buttons;
-(int)buttonCount;
@end
static id mySheet=nil;
static id hud=nil;
static UIImage * grayscaleImage(UIImage *image,int orientation){
BOOL IOS5= [NSFileManager instancesRespondToSelector:@selector(URLForUbiquityContainerIdentifier:)];
CGImageRef rotatedImage=orientation !=0 ? (IOS5 ? (CGImageRef)[objc_getClass("PLManagedAsset") newUnrotatedImageWithSize:image.size originalImage:[image CGImage] imageOrientation: image.imageOrientation+1] : (CGImageRef)[objc_getClass("MLPhoto") createUnrotatedImageWithSize:image.size originalImage:[image CGImage] imageOrientation:orientation+1] ): [image CGImage];
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
CGContextDrawImage(context, imageRect,rotatedImage);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:imageRef ];
CGColorSpaceRelease(colorSpace);
if (orientation !=0){
CFRelease(rotatedImage);
}
CFRelease(imageRef);
CGContextRelease(context);
return newImage;
}
%group IOS5
%hook PLPhonePhotoScrollerViewController
%new(v@:@@^v)-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *) error contextInfo:(void*)contextInfo{
if (error && hud){
[hud setText:@"Error converting image!"];
}
else if (!error && hud){
[hud setText:@"Converted!"];
[hud done];
}
if (hud)
[hud performSelector:@selector(hide) withObject:nil afterDelay:1.2];
}
-(void)actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(int)anIndex{
if ([sheet isEqual:mySheet] && [[sheet buttonAtIndex:anIndex] isEqual:[[sheet buttons] lastObject]]){
hud=[[UIProgressHUD alloc] init] ;
[hud setText:@"Converting Image..."];
[hud showInView:[[self view] window]];
}
if (anIndex==sheet.numberOfButtons-1){
anIndex=sheet.cancelButtonIndex-1;
}
%orig;
}
-(void)actionSheet:(UIActionSheet *)sheet didDismissWithButtonIndex:(int)anIndex{
if ([sheet isEqual:mySheet] && [[sheet buttonAtIndex:anIndex] isEqual:[[sheet buttons] lastObject]]){
NSString *imageFile=[[[self currentTile] photo] pathForOriginalFile];
UIImage *image=[UIImage imageWithContentsOfFile:imageFile];
int orientation=(int)[[[self currentTile] photo] orientationValue];
UIImageWriteToSavedPhotosAlbum (grayscaleImage(image,orientation), self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
return;
}
if (anIndex==sheet.numberOfButtons-1){
anIndex=sheet.cancelButtonIndex-1;
}
%orig;
}
%end
%end
%group newerVersions
%hook PLPhotoScrollerViewController
%new(v@:@@^v)-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *) error contextInfo:(void*)contextInfo{
if (error && hud){
[hud setText:@"Error converting image!"];
}
else if (!error && hud){
[hud setText:@"Converted!"];
[hud done];
}
if (hud)
[hud performSelector:@selector(hide) withObject:nil afterDelay:1.2];
}
-(void)actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(int)anIndex{
if ([sheet isEqual:mySheet] && [[sheet buttonAtIndex:anIndex] isEqual:[[sheet buttons] lastObject]]){
hud=[[UIProgressHUD alloc] init] ;
[hud setText:@"Converting Image..."];
[hud showInView:[[self currentImageView] window]];
}
if (anIndex==sheet.numberOfButtons-1){
anIndex=sheet.cancelButtonIndex-1;
}
%orig;
}
-(void)actionSheet:(UIActionSheet *)sheet didDismissWithButtonIndex:(int)anIndex{
if ([sheet isEqual:mySheet] && [[sheet buttonAtIndex:anIndex] isEqual:[[sheet buttons] lastObject]]){
NSString *imageFile=[[self currentImage] pathForOriginalFile];
UIImage *image=[UIImage imageWithContentsOfFile:imageFile];
UIImage *currentUIImage=[self currentUIImage];
int orientation=[currentUIImage imageOrientation];
UIImageWriteToSavedPhotosAlbum (grayscaleImage(image,orientation), self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
return;
}
if (anIndex==sheet.numberOfButtons-1){
anIndex=sheet.cancelButtonIndex-1;
}
%orig;
}
%end
%end
%group olderVersions
%hook PLAlbumsController
%new(v@:@@^v)-(void)myImage:(UIImage *)image didFinishSavingWithError:(NSError *) error contextInfo:(void*)contextInfo{
if (error && hud){
[hud setText:@"Error converting image!"];
}
else if (!error && hud){
[hud setText:@"Converted!"];
[hud done];
}
if (hud)
[hud performSelector:@selector(hide) withObject:nil afterDelay:1.2];
}
-(void)actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(int)anIndex{
if ([sheet isEqual:mySheet] && [[sheet buttonAtIndex:anIndex] isEqual:[[sheet buttons] lastObject]]){
hud=[[UIProgressHUD alloc] init] ;
[hud setText:@"Converting Image..."];
[hud showInView:[[self currentImageView] window]];
}
if (anIndex==sheet.numberOfButtons-1){
anIndex=sheet.cancelButtonIndex-1;
}
%orig;
}
-(void)actionSheet:(UIActionSheet *)sheet didDismissWithButtonIndex:(int)anIndex{
if ([sheet isEqual:mySheet] && [[sheet buttonAtIndex:anIndex] isEqual:[[sheet buttons] lastObject]]){
NSString *imageFile=[[self currentImage] pathForOriginalFile];
UIImage *image=[UIImage imageWithContentsOfFile:imageFile];
UIImage *currentUIImage=[[self currentUIImage] retain];
int orientation=[currentUIImage imageOrientation] ;
UIImageWriteToSavedPhotosAlbum (grayscaleImage(image,orientation), self, @selector(myImage:didFinishSavingWithError:contextInfo:), nil);
}
if (anIndex==sheet.numberOfButtons-1){
anIndex=sheet.cancelButtonIndex-1;
}
%orig;
}
%end
%end
%hook UIActionSheet
-(void)presentSheetInView:(id)view {
if ([[[[self buttons] objectAtIndex:0] title] isEqualToString:[[NSBundle bundleWithIdentifier:@"com.apple.PhotoLibrary"] localizedStringForKey:@"SEND_PHOTO_VIA_EMAIL_BUTTON" value:nil table:@"Main"]] || [[[[self buttons] objectAtIndex:0] title] isEqualToString:[[NSBundle bundleWithIdentifier:@"com.apple.PhotoLibrary"] localizedStringForKey:@"SEND_PHOTO_VIA_EMAIL_BUTTON" value:nil table:@"PhotoLibrary"]]) {
mySheet=self;
[self addButtonWithTitle:@"Convert to Grayscale"];
UIThreePartButton *converButton= [[self buttons] lastObject];
[[self buttons] removeObject:converButton];
[[self buttons] insertObject:converButton atIndex:0];
self.cancelButtonIndex = self.numberOfButtons-1;
}
%orig;
}
%end
__attribute__((constructor)) void GrayPixInit(){
%init;
if (!objc_getClass("MLPhoto") && [NSFileManager instancesRespondToSelector:@selector(URLForUbiquityContainerIdentifier:)]){
%init(IOS5);
}
else if ([objc_getClass("MLPhoto") instancesRespondToSelector:@selector(pathForImageFile)]){
%init(newerVersions);
}
else{
%init(olderVersions);
}
}