Skip to content
This repository was archived by the owner on Dec 2, 2019. It is now read-only.

Implement quality option for ios #24

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ RNThumbnail.get(filepath).then((result) => {
console.log(result.path); // thumbnail path
})
```

### Options

`RNThumbnail.get` supports a second argument which is a object with the following possible options:

- `timestamp` (currently `ios` only): The timestamp of the thumbnail in seconds (calculates the frame based on the FPS of the video). Default Value: `0`
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@

import { NativeModules } from 'react-native';
import { NativeModules, Platform } from 'react-native';

const { RNThumbnail } = NativeModules;
const { RNThumbnail: NativeRNThumbnail } = NativeModules;

export default RNThumbnail;
const defaultOptions = {
timestamp: 0,
quality: 1
}

export default class RNThumbnail {
static get(path, options = {}) {
if (Platform.OS === 'ios') {
const mergedOptions = {...defaultOptions, ...options}
return NativeRNThumbnail.get(path, mergedOptions.timestamp, mergedOptions.quality)
}
return NativeRNThumbnail.get(path)
}
};
18 changes: 13 additions & 5 deletions ios/RNThumbnail.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,29 @@ - (dispatch_queue_t)methodQueue
}
RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(get:(NSString *)filepath resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
RCT_EXPORT_METHOD(get:(NSString *)filepath
timestamp:(NSNumber * __nonnull)timestamp
quality:(NSNumber * __nonnull)quality
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 = 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(1, 60);
CMTime time = CMTimeMake(value, 60);

CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err];
UIImage *thumbnail = [UIImage imageWithCGImage:imgRef];
Expand All @@ -34,7 +43,7 @@ - (dispatch_queue_t)methodQueue
NSUserDomainMask,
YES) lastObject];

NSData *data = UIImageJPEGRepresentation(thumbnail, 1.0);
NSData *data = UIImageJPEGRepresentation(thumbnail, [quality floatValue]);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fullPath = [tempDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"thumb-%@.jpg", [[NSProcessInfo processInfo] globallyUniqueString]]];
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
Expand All @@ -48,4 +57,3 @@ - (dispatch_queue_t)methodQueue
}

@end