#import "RNThumbnail.h" #import #import #import @implementation RNThumbnail - (dispatch_queue_t)methodQueue { return dispatch_get_main_queue(); } RCT_EXPORT_MODULE() RCT_EXPORT_METHOD(get:(NSString *)filepath resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) { @try { filepath = [filepath stringByReplacingOccurrencesOfString:@"file://" withString:@""]; NSURL *vidURL = [NSURL fileURLWithPath:filepath]; AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL options:nil]; AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; generator.appliesPreferredTrackTransform = YES; NSError *err = NULL; CMTime time = CMTimeMake(1, 60); CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err]; UIImage *thumbnail = [UIImage imageWithCGImage:imgRef]; // save to temp directory NSString* tempDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSData *data = UIImageJPEGRepresentation(thumbnail, 1.0); NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *fullPath = [tempDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"thumb-%@.jpg", [[NSProcessInfo processInfo] globallyUniqueString]]]; [fileManager createFileAtPath:fullPath contents:data attributes:nil]; CGImageRelease(imgRef); if (resolve) resolve(@{ @"path" : fullPath, @"width" : [NSNumber numberWithFloat: thumbnail.size.width], @"height" : [NSNumber numberWithFloat: thumbnail.size.height] }); } @catch(NSException *e) { reject(e.reason, nil, nil); } } @end