Two Methods for capturing video thumbnails in iOS:
After you have finished playing an online video using MPMoviePlayerController, you may need to obtain a thumbnail of the video in practice. Let's take a look at how to take a thumbnail of the video within the specified time.
1. Use the built-in methods of MPMoviePlayerController
- (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option NS_AVAILABLE_IOS(3_2);
/*** Video */-(void) getMovieThumImage {[self. moviePlayer requestThumbnailImagesAtTimes: @ [@ (1.0), @ (5.0)] // set timeOption: MPMovieTimeOptionNearestKeyFrame when the time is 1 s and 5s];}
[[Nsicationicationcenter defacenter center] addObserver: self selector: @ selector (thumImageGet :) name: MPMoviePlayerThumbnailImageRequestDidFinishNotification // call object: nil when the video thumbnail is intercepted successfully];
/*** After each screenshot is completed, it is retrieved once ** @ param noti <# noti description #> */-(void) thumImageGet :( NSNotification *) noti {UIImage * thumImage = [[noti userInfo] objectForKey: MPMoviePlayerThumbnailImageKey]; UIImageWriteToSavedPhotosAlbum (thumImage, nil );}
2. AVAssetImageGenerator
- Create an AVURLAsset object (this class is mainly used to obtain media information, including videos and sounds ).
- Create an AVAssetImageGenerator object based on AVURLAsset.
- Use copyCGImageAtTime: Of AVAssetImageGenerator to obtain the specified time point.
#import <AVFoundation/AVFoundation.h>
<Pre name = "code" class = "objc">-(void) assetGetThumImage :( CGFloat) second {AVURLAsset * urlSet = [AVURLAsset assetWithURL: [self url]; AVAssetImageGenerator * imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset: urlSet]; NSError * error = nil; CMTime time = CMTimeMake (second, 10 ); // The thumbnail creation time, CMTime, is the struct that represents the Time of the movie. The first parameter indicates the second of the video, and the second parameter indicates the number of frames per second. (The CMTimeMake method can be used if you want to live the first frame of a second.) CMTime actucalTime; // The actual generation time of the thumbnail CGImageRef cgImage = [imageGenerator copyCGImageAtTime: time actualTime: & actucalTime error: & error]; if (error) {NSLog (@ "failed to intercept the video image: % @", error. localizedDescription);} CMTimeShow (actucalTime); UIImage * image = [UIImage imageWithCGImage: cgImage]; UIImageWriteToSavedPhotosAlbum (image, nil); cgimagerel.pdf (cgImage ); NSLog (@ "video intercepted ");}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.