Two ways to capture video thumbnails in iOS

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:24px">preface:</span> <span style="font-size:18px; background-color:rgb(255,255,255)">after watching the <span style="color:#3366ff">use of MPMoviePlayerController play online video</span> , in the actual application sometimes need to get the thumbnail image of the video, we can see how to intercept the video thumbnail in the specified time. </span></p></p><p><p><br></p></p><p><p><span style="font-size:18px">A <span style="color:rgb(51,51,51); font-family:Georgia,‘Times New Roman‘,Times,sans-serif; line-height:25px">method of using MPMoviePlayerController</span></span></p></p><p><p><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:Georgia,‘Times New Roman‘,Times,sans-serif; line-height:25px"><br></span></span></p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">-(void) requestthumbnailimagesattimes: (nsarray *) playbacktimes timeoption: (mpmovietimeoption) option NS_AVAILABLE_ IOS (3_2);</pre></pre><br><br><p><p></p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">/** * video */-(void) getmoviethumimage{ [self.movieplayer requestthumbnailimagesattimes:@[@ (1.0), @ (5.0)]// Set the time point 1s and 5s when timeoption:mpmovietimeoptionnearestkeyframe];}</pre></pre><br><pre name="code" class="objc"><pre name="code" class="objc">[[nsnotificationcenter defaultcenter] addobserver:self selector: @selector (thumimageget:) name: Mpmovieplayerthumbnailimagerequestdidfinishnotification //video thumbnail capture successful call object:nil];</pre></pre><br><pre name="code" class="objc"><pre name="code" class="objc">/** * completed, each interception of one, will be adjusted once * * @param noti < #noti description#> */-(void) thumimageget: (nsnotification *) noti{< C2/>uiimage *thumimage = [[noti userInfo] objectforkey:mpmovieplayerthumbnailimagekey]; Uiimagewritetosavedphotosalbum (thumimage, nil, nil, nil);}</pre></pre><span style="font-size:18px"><span style="font-size:18px"><br>Two use Avassetimagegenerator</span></span><p><p></p></p><p><p><span style="font-size:18px"></span></p></p> <ol style="padding-left:50px; color:rgb(51,51,51); font-family:Georgia,‘Times New Roman‘,Times,sans-serif; font-size:14px; line-height:25px"> <ol style="padding-left:50px; color:rgb(51,51,51); font-family:Georgia,‘Times New Roman‘,Times,sans-serif; font-size:14px; line-height:25px"> <li style="padding:0px; list-style-type:decimal">Create Avurlasset Objects (this class is primarily used to obtain media information, including video, sound, and so on).</li> <li style="padding:0px; list-style-type:decimal">Creates a Avassetimagegenerator object from Avurlasset.</li> <li style="padding:0px; list-style-type:decimal">Use Avassetimagegenerator's copycgimageattime:: method to get the specified point in Time.</li> </ol> </ol><br><p><p></p></p><p><p><span style="font-size:18px"></span></p></p><pre name="code" class="objc"><pre name="code" class="objc">#import <AVFoundation/AVFoundation.h></pre></pre><pre name="code" class="objc"><pre name="code" class="objc"><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);//thumbnail creation times Cmtime is the structure that represents the movie time information, the first parameter is the video seconds, The second parameter represents the number of frames per second. (you can use the Cmtimemake method if you want to live the first few frames of a second) Cmtime actucaltime; Thumbnail actual generated time cgimageref cgimage = [imagegenerator copycgimageattime:time actualtime:&actucaltime error:& error]; If (error) { NSLog (@ "intercept video picture failed:%@", error.localizeddescription); } Cmtimeshow (actucaltime); UIImage *image = [UIImage imagewithcgimage:cgimage]; Uiimagewritetosavedphotosalbum (image,nil, nil,nil); Cgimagerelease (cgimage); NSLog (@ "video Capture success");}</pre></pre><br><p><p></p></p><pre><pre></pre></pre><p><p></p></p><p><p><br></p></p><br> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Two ways to capture video thumbnails in iOS</p></p></span>

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.