No Description

RNThumbnail.m 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #import "RNThumbnail.h"
  2. #import <AVFoundation/AVFoundation.h>
  3. #import <AVFoundation/AVAsset.h>
  4. #import <UIKit/UIKit.h>
  5. @implementation RNThumbnail
  6. - (dispatch_queue_t)methodQueue
  7. {
  8. return dispatch_get_main_queue();
  9. }
  10. RCT_EXPORT_MODULE()
  11. RCT_EXPORT_METHOD(get:(NSString *)filepath resolve:(RCTPromiseResolveBlock)resolve
  12. reject:(RCTPromiseRejectBlock)reject)
  13. {
  14. @try {
  15. filepath = [filepath stringByReplacingOccurrencesOfString:@"file://"
  16. withString:@""];
  17. NSURL *vidURL = [NSURL fileURLWithPath:filepath];
  18. AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL options:nil];
  19. AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
  20. generator.appliesPreferredTrackTransform = YES;
  21. NSError *err = NULL;
  22. CMTime time = CMTimeMake(1, 60);
  23. CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err];
  24. UIImage *thumbnail = [UIImage imageWithCGImage:imgRef];
  25. // save to temp directory
  26. NSString* tempDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
  27. NSUserDomainMask,
  28. YES) lastObject];
  29. NSData *data = UIImageJPEGRepresentation(thumbnail, 1.0);
  30. NSFileManager *fileManager = [NSFileManager defaultManager];
  31. NSString *fullPath = [tempDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"thumb-%@.jpg", [[NSProcessInfo processInfo] globallyUniqueString]]];
  32. [fileManager createFileAtPath:fullPath contents:data attributes:nil];
  33. CGImageRelease(imgRef);
  34. if (resolve)
  35. resolve(@{ @"path" : fullPath,
  36. @"width" : [NSNumber numberWithFloat: thumbnail.size.width],
  37. @"height" : [NSNumber numberWithFloat: thumbnail.size.height] });
  38. } @catch(NSException *e) {
  39. reject(e.reason, nil, nil);
  40. }
  41. }
  42. @end