Two Methods for capturing video thumbnails in iOS:

Source: Internet
Author: User

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

  1. Create an AVURLAsset object (this class is mainly used to obtain media information, including videos and sounds ).
  2. Create an AVAssetImageGenerator object based on AVURLAsset.
  3. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.