-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.xm
120 lines (90 loc) · 3.77 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
#import "BJFilePickerController.h"
#import "BJImageWrapper.h"
#import "BJAssetWrapper.h"
#import "BJFetchResultWrapper.h"
@interface PUUIAlbumListViewController : UIViewController
@end
// add button listener
%hook UIImagePickerController
%new
- (void)_pushFilePicker {
[self pushViewController:[BJFilePickerController new] animated:YES];
}
%end
%hook PUUIAlbumListViewController
- (void)updateNavigationBarAnimated:(BOOL)animated {
%orig;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"File" style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(_pushFilePicker)];
}
%end
// swizzling PHFetchResults
%hook PHAsset
+ (PHFetchResult<PHAsset *> *)fetchAssetsWithALAssetURLs:(NSArray<NSURL *> *)assetURLs options:(PHFetchOptions *)options {
if (assetURLs.count == 1) {
NSURL *target = assetURLs.firstObject;
if ([target.scheme isEqualToString:@"file"]) {
return [BJFetchResultWrapper resultWithPath:target.path];
}
}
return %orig;
}
%end
%hook PHImageManager
// PHAsset to UIImage
- (PHImageRequestID)requestImageForAsset:(BJAssetWrapper *)asset targetSize:(CGSize)targetSize contentMode:(PHImageContentMode)contentMode options:(PHImageRequestOptions *)options resultHandler:(void (^)(UIImage *result, NSDictionary *info))resultHandler {
if ([asset isKindOfClass:BJAssetWrapper.class]) {
NSString *filePath = asset.realWrapped;
BJImageWrapper *fakeImage = [BJImageWrapper wrapperWithPath:filePath];
NSDictionary *returnInfo = @{
@"PHImageFileURLKey" : [NSURL fileURLWithPath:filePath],
@"PHImageResultIsPlaceholderKey" : @NO,
@"PHImageResultOptimizedForSharing" : @YES,
// required keys
PHImageResultIsDegradedKey : @NO,
PHImageResultIsInCloudKey : @NO,
PHImageResultRequestIDKey : @(0)
};
resultHandler(fakeImage, returnInfo);
return 0;
}
return %orig;
}
// PHAsset to NSData
- (PHImageRequestID)requestImageDataForAsset:(BJAssetWrapper *)asset options:(PHImageRequestOptions *)options resultHandler:(void(^)(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info))resultHandler {
if ([asset isKindOfClass:BJAssetWrapper.class]) {
// https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
NSString *utiKey = @"public.data";
UIImageOrientation orientationKey = 0;
NSDictionary *returnInfo = @{
@"PHImageFileURLKey" : [NSURL fileURLWithPath:asset.realWrapped],
@"PHImageResultIsPlaceholderKey" : @NO,
@"PHImageResultOptimizedForSharing" : @YES,
// data specific
@"PHImageFileOrientationKey" : @(orientationKey),
@"PHImageFileUTIKey" : utiKey,
// required keys
PHImageResultIsDegradedKey : @NO,
PHImageResultIsInCloudKey : @NO,
PHImageResultRequestIDKey : @(0)
};
resultHandler(asset.realData, utiKey, orientationKey, returnInfo);
return 0;
}
return %orig;
}
%end
// UIImage to NSData
%hookf(NSData *, UIImagePNGRepresentation, BJImageWrapper *image) {
if ([image isKindOfClass:BJImageWrapper.class]) {
return image.realData;
}
return %orig;
}
// This hook currently results in SpringBoard failing to load
// %hookf(NSData *, UIImageJPEGRepresentation, BJImageWrapper *image, CGFloat compressionQuality) {
// if ([image isKindOfClass:BJImageWrapper.class]) {
// return image.realData;
// }
//
// return %orig;
// }