@@ -12,42 +12,44 @@ - (dispatch_queue_t)methodQueue
12
12
}
13
13
RCT_EXPORT_MODULE ()
14
14
15
- RCT_EXPORT_METHOD(get:(NSString *)filepath timestamp:(NSNumber * __nonnull)timestamp resolve:(RCTPromiseResolveBlock)resolve
15
+ RCT_EXPORT_METHOD(get:(NSString *)filepath
16
+ timestamp:(NSNumber * __nonnull)timestamp
17
+ resolve:(RCTPromiseResolveBlock)resolve
16
18
reject:(RCTPromiseRejectBlock)reject)
17
19
{
18
20
@try {
19
21
filepath = [filepath stringByReplacingOccurrencesOfString: @" file://"
20
- withString: @" " ];
22
+ withString: @" " ];
21
23
NSURL *vidURL = [NSURL fileURLWithPath: filepath];
22
-
24
+
23
25
AVURLAsset *asset = [[AVURLAsset alloc ] initWithURL: vidURL options: nil ];
24
- int64_t value = 0 ;
26
+ int64_t value = 1 ;
25
27
if (timestamp != 0 ) {
26
28
AVAssetTrack * videoAssetTrack = [asset tracksWithMediaType: AVMediaTypeVideo].firstObject ;
27
29
value = [timestamp longLongValue ] * videoAssetTrack.nominalFrameRate ;
28
30
}
29
-
31
+
30
32
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc ] initWithAsset: asset];
31
33
generator.appliesPreferredTrackTransform = YES ;
32
-
34
+
33
35
NSError *err = NULL ;
34
36
CMTime time = CMTimeMake (value, 60 );
35
-
37
+
36
38
CGImageRef imgRef = [generator copyCGImageAtTime: time actualTime: NULL error: &err];
37
39
UIImage *thumbnail = [UIImage imageWithCGImage: imgRef];
38
40
// save to temp directory
39
41
NSString * tempDirectory = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory,
40
- NSUserDomainMask,
41
- YES ) lastObject ];
42
-
42
+ NSUserDomainMask,
43
+ YES ) lastObject ];
44
+
43
45
NSData *data = UIImageJPEGRepresentation (thumbnail, 1.0 );
44
46
NSFileManager *fileManager = [NSFileManager defaultManager ];
45
47
NSString *fullPath = [tempDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @" thumb-%@ .jpg" , [[NSProcessInfo processInfo ] globallyUniqueString ]]];
46
48
[fileManager createFileAtPath: fullPath contents: data attributes: nil ];
47
49
if (resolve)
48
- resolve (@{ @" path" : fullPath,
49
- @" width" : [NSNumber numberWithFloat: thumbnail.size.width],
50
- @" height" : [NSNumber numberWithFloat: thumbnail.size.height] });
50
+ resolve (@{ @" path" : fullPath,
51
+ @" width" : [NSNumber numberWithFloat: thumbnail.size.width],
52
+ @" height" : [NSNumber numberWithFloat: thumbnail.size.height] });
51
53
} @catch (NSException *e) {
52
54
reject (e.reason , nil , nil );
53
55
}
0 commit comments