-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathMerryPhotoView.m
336 lines (285 loc) · 11.3 KB
/
MerryPhotoView.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Created by react-native-create-bridge
#import "MerryPhotoView.h"
#import <Foundation/Foundation.h>
@implementation MerryPhotoView {
__weak RCTBridge* _bridge;
}
//@synthesize bridge = _bridge;
- (instancetype)initWithBridge:(RCTBridge*)bridge
{
if ((self = [super init])) {
_bridge = bridge;
}
return self;
}
- (void)didMoveToWindow
{
if (self.data) {
[self showViewer:self.data];
}
}
- (void)removeFromSuperview
{
[self hideViewer];
}
- (void)invalidate
{
[self hideViewer];
}
- (void)clean
{
self.nytPhotosViewController = nil;
self.dataSource = nil;
self.photos = nil;
self.data = nil;
self.reactPhotos = nil;
}
/**
Get root view
@param rootViewController <#rootViewController description#>
@return presented View
*/
- (UIViewController*)visibleViewController:(UIViewController*)rootViewController
{
if (rootViewController.presentedViewController == nil) {
return rootViewController;
}
if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController.presentedViewController;
UIViewController* lastViewController = [[navigationController viewControllers] lastObject];
return [self visibleViewController:lastViewController];
}
if ([rootViewController.presentedViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController.presentedViewController;
UIViewController* selectedViewController = tabBarController.selectedViewController;
return [self visibleViewController:selectedViewController];
}
UIViewController* presentedViewController = (UIViewController*)rootViewController.presentedViewController;
return [self visibleViewController:presentedViewController];
}
- (UIViewController*)getRootView
{
UIViewController* rootView =
[self visibleViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
return rootView;
}
- (void)hideViewer
{
[self.nytPhotosViewController dismissViewControllerAnimated:YES completion:nil];
[self clean];
}
- (void)showViewer:(NSArray*)data
{
if (!data) {
// reject(@"9527", @"Display photo viewer failed, please config it first", nil);
return;
}
NSMutableArray* msPhotos = [NSMutableArray array];
NSMutableArray* rsPhotos = [NSMutableArray array];
for (NSDictionary* dic in data) {
MerryPhotoData* d = [[MerryPhotoData alloc] initWithDictionary:dic];
MerryPhoto* merryPhoto = [MerryPhoto new];
merryPhoto.image = nil;
if (d.title) {
merryPhoto.attributedCaptionTitle = [MerryPhotoView attributedTitleFromString:d.title:d.titleColor ? [RCTConvert UIColor:d.titleColor] : [UIColor whiteColor]];
}
if (d.summary) {
merryPhoto.attributedCaptionSummary = [MerryPhotoView attributedSummaryFromString:d.summary:d.summaryColor ? [RCTConvert UIColor:d.summaryColor] : [UIColor lightGrayColor]];
}
[msPhotos addObject:merryPhoto];
[rsPhotos addObject:d];
}
self.photos = msPhotos;
self.reactPhotos = rsPhotos;
self.dataSource = [NYTPhotoViewerArrayDataSource dataSourceWithPhotos:self.photos];
NSInteger initialPhoto = self.initial;
dispatch_async(dispatch_get_main_queue(), ^{
@try {
NYTPhotosViewController* photosViewController = [[NYTPhotosViewController alloc]
initWithDataSource:self.dataSource
initialPhoto:[self.photos objectAtIndex:initialPhoto]
delegate:self];
// TODO add options for:
// hide left bar button
if (self.hideCloseButton) {
photosViewController.leftBarButtonItem = nil;
}
// hide right bar button
if (self.hideShareButton) {
photosViewController.rightBarButtonItem = nil;
}
self.nytPhotosViewController = photosViewController;
[[self getRootView] presentViewController:photosViewController
animated:YES
completion:nil];
if (initialPhoto >= 0) {
[self updatePhotoAtIndex:photosViewController Index:initialPhoto];
}
if (self.hideStatusBar) {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:YES];
}
// resolve(@"");
} @catch (NSException* exception) {
// reject(@"9527", @"Display photo viewer failed, please check your configurations", nil);
} @finally {
}
});
}
+ (NSString*)contentTypeForImageData:(NSData*)data
{
uint8_t c;
[data getBytes:&c length:1];
switch (c) {
case 0xFF:
return @"jpeg";
case 0x89:
return @"png";
case 0x47:
return @"gif";
case 0x49:
case 0x4D:
return @"tiff";
}
return nil;
}
/**
Update Photo
@param photosViewController <#photosViewController description#>
@param photoIndex <#photoIndex description#>
*/
- (void)updatePhotoAtIndex:(NYTPhotosViewController*)photosViewController
Index:(NSUInteger)photoIndex
{
NSInteger current = (unsigned long)photoIndex;
MerryPhoto* currentPhoto = [self.dataSource.photos objectAtIndex:current];
MerryPhotoData* d = self.reactPhotos[current];
//
// NSString* url = d.url;
// NSURL* imageURL = [NSURL URLWithString:url];
// check an url is a gif image.
// NOTE: this check require your url have an extension.
// BOOL isGif = [[imageURL pathExtension] isEqual:@"gif"];
[_bridge.imageLoader loadImageWithURLRequest:d.source.request
size:d.source.size
scale:d.source.scale
clipped:YES
resizeMode:RCTResizeModeStretch
progressBlock:^(int64_t progress, int64_t total) {
// NSLog(@"%lld %lld", progress, total);
}
partialLoadBlock:nil
completionBlock:^(NSError* error, UIImage* image) {
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
// NSData *data = UIImagePNGRepresentation(image);
// NSString* ext = [MerryPhotoView contentTypeForImageData:data];
// BOOL isGif = ext && [ext isEqualToString:@"gif"];
// if(isGif){
// currentPhoto.imageData = data;
// }else{
// currentPhoto.image = image;
// }
//
currentPhoto.image = image;
[photosViewController updatePhoto:currentPhoto];
});
}
}];
}
#pragma mark - NYTPhotosViewControllerDelegate
- (UIView*)photosViewController:(NYTPhotosViewController*)photosViewController loadingViewForPhoto:(id<NYTPhoto>)photo
{
return nil;
}
- (UIView*)photosViewController:(NYTPhotosViewController*)photosViewController
referenceViewForPhoto:(id<NYTPhoto>)photo
{
return nil;
}
/**
Customize title display
@param photosViewController <#photosViewController description#>
@param photo <#photo description#>
@param photoIndex <#photoIndex description#>
@param totalPhotoCount <#totalPhotoCount description#>
@return <#return value description#>
*/
- (NSString*)photosViewController:(NYTPhotosViewController*)photosViewController
titleForPhoto:(id<NYTPhoto>)photo
atIndex:(NSInteger)photoIndex
totalPhotoCount:(nullable NSNumber*)totalPhotoCount
{
return [NSString stringWithFormat:@"%lu / %lu", (unsigned long)photoIndex + 1,
(unsigned long)totalPhotoCount.integerValue];
}
/**
Download current photo if its nil
@param photosViewController <#photosViewController description#>
@param photo <#photo description#>
@param photoIndex <#photoIndex description#>
*/
- (void)photosViewController:(NYTPhotosViewController*)photosViewController
didNavigateToPhoto:(id<NYTPhoto>)photo
atIndex:(NSUInteger)photoIndex
{
if (!photo.image || !photo.imageData) {
[self updatePhotoAtIndex:photosViewController Index:photoIndex];
}
// NOTE: onChange are useless so we don't plan to implementing it.
// MerryPhotoData* current = [self.reactPhotos objectAtIndex:photoIndex];
//
// if (self.onNavigateToPhoto) {
// self.onNavigateToPhoto(@{
// @"currentPhoto" : current
// });
// }
}
- (void)displayActivityViewController:(UIActivityViewController*)controller animated:(BOOL)animated
{
[[self getRootView] presentViewController:controller animated:animated completion:nil];
}
- (BOOL)photosViewController:(NYTPhotosViewController*)photosViewController handleLongPressForPhoto:(id<NYTPhoto>)photo withGestureRecognizer:(UILongPressGestureRecognizer*)longPressGestureRecognizer
{
if ((photosViewController.currentlyDisplayedPhoto.image || photosViewController.currentlyDisplayedPhoto.imageData)) {
UIImage* image = photosViewController.currentlyDisplayedPhoto.image ? photosViewController.currentlyDisplayedPhoto.image : [UIImage imageWithData:photosViewController.currentlyDisplayedPhoto.imageData];
UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[ image ] applicationActivities:nil];
activityViewController.completionWithItemsHandler = ^(NSString* __nullable activityType, BOOL completed, NSArray* __nullable returnedItems, NSError* __nullable activityError) {
if (completed && [photosViewController.delegate respondsToSelector:@selector(photosViewController:actionCompletedWithActivityType:)]) {
[photosViewController.delegate photosViewController:photosViewController actionCompletedWithActivityType:activityType];
}
};
[self displayActivityViewController:activityViewController animated:YES];
}
return YES;
}
- (void)photosViewControllerDidDismiss:(NYTPhotosViewController*)photosViewController
{
if (self.hideStatusBar) {
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
}
if (self.onDismiss) {
self.onDismiss(nil);
}
[self clean];
}
+ (NSAttributedString*)attributedTitleFromString:(NSString*)caption
:(UIColor*)color
{
return [[NSAttributedString alloc]
initWithString:caption
attributes:@{
NSForegroundColorAttributeName : color,
NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody]
}];
}
+ (NSAttributedString*)attributedSummaryFromString:(NSString*)summary
:(UIColor*)color
{
return [[NSAttributedString alloc]
initWithString:summary
attributes:@{
NSForegroundColorAttributeName : color,
NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody]
}];
}
@end