iOS Cut video footage into thumbnails
- 2021-07-09 09:24:14
- OfStack
In this paper, we share the specific code of cutting iOS video into thumbnails for your reference. The specific contents are as follows
Remember to import the system library
#import < MediaPlayer/MediaPlayer.h >
Code:
/**
* Method of obtaining all thumbnails of network video
*
* @param videoURL Link address of video
*
* @return Video screenshot
*/
+ (UIImage *)ihefe_previewImageWithVideoURL:(NSURL *)videoURL
{
AVAsset *asset = [AVAsset assetWithURL:videoURL];
AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;
CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(1, asset.duration.timescale) actualTime:NULL error:nil];
UIImage *image = [UIImage imageWithCGImage:img];
CGImageRelease(img);
return image;
}
/**
* Method for obtaining all thumbnails of local video
*
* @param fileurl Link address of video
*
* @return Video screenshot
*/
+ (UIImage *)ihefe_getScreenShotImageFromVideoURL:(NSString *)fileurl
{
UIImage *shotImage;
// Video path URL
NSURL *fileURL = [NSURL URLWithString:fileurl];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
shotImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return shotImage;
}
/**
* Gets some of the video 1 Frame thumbnail method
*
* @param videoURL Link address of video Frame time
* @param time Frame time
*
* @return Video screenshot
*/
+ (UIImage*)ihefe_thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetImageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
assetImageGenerator.appliesPreferredTrackTransform = YES;
assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *thumbnailImageGenerationError = nil;
thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&thumbnailImageGenerationError];
if (!thumbnailImageRef) NSLog(@"thumbnailImageGenerationError %@", thumbnailImageGenerationError);
UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] : nil;
return thumbnailImage;
}