Nav apraksta

RNThumbnail.m 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. if (resolve)
  34. resolve(@{ @"path" : fullPath,
  35. @"width" : [NSNumber numberWithFloat: thumbnail.size.width],
  36. @"height" : [NSNumber numberWithFloat: thumbnail.size.height] });
  37. } @catch(NSException *e) {
  38. reject(e.reason, nil, nil);
  39. }
  40. }
  41. @end