diff --git a/ios/RNThumbnail.m b/ios/RNThumbnail.m index 61e7950..e1747c9 100644 --- a/ios/RNThumbnail.m +++ b/ios/RNThumbnail.m @@ -12,42 +12,44 @@ - (dispatch_queue_t)methodQueue } RCT_EXPORT_MODULE() -RCT_EXPORT_METHOD(get:(NSString *)filepath timestamp:(NSNumber * __nonnull)timestamp resolve:(RCTPromiseResolveBlock)resolve +RCT_EXPORT_METHOD(get:(NSString *)filepath + timestamp:(NSNumber * __nonnull)timestamp + 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]; - int64_t value = 0; + int64_t value = 1; if (timestamp != 0) { AVAssetTrack * videoAssetTrack = [asset tracksWithMediaType: AVMediaTypeVideo].firstObject; value = [timestamp longLongValue] * videoAssetTrack.nominalFrameRate; } - + AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; generator.appliesPreferredTrackTransform = YES; NSError *err = NULL; CMTime time = CMTimeMake(value, 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]; - + 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]; if (resolve) - resolve(@{ @"path" : fullPath, - @"width" : [NSNumber numberWithFloat: thumbnail.size.width], - @"height" : [NSNumber numberWithFloat: thumbnail.size.height] }); + resolve(@{ @"path" : fullPath, + @"width" : [NSNumber numberWithFloat: thumbnail.size.width], + @"height" : [NSNumber numberWithFloat: thumbnail.size.height] }); } @catch(NSException *e) { reject(e.reason, nil, nil); }