IOS Video Capture

Source: Internet
Author: User

Recently developed an app, need a lot of knowledge of this, so wrote this document, hoping to help some need to consult the small partners, basically is the code snippets, there is no conceptual knowledge.

Capture part of a picture
-(UIImage *) Getpartofimage: (UIImage *) img rect: (cgrect) partrect      {      = img. cgimage;       = Cgimagecreatewithimageinrect (imageref, partrect);       *retimg = [UIImage imagewithcgimage:imagepartref];      Cgimagerelease (imagepartref);       return retimg;      }  

iOS screen

Normal cannot intercept video rendered through OpenGL, so the interception of video is not possible using the following methods, it is necessary for the small partners to find other OpenGL methods.

 Uigraphicsbeginimagecontextwithoptions (_videorect.size, YES, 0);    Set the screenshot size [[_movieplayercontroller.view layer] renderincontext:uigraphicsgetcurrentcontext ()];    UIImage *viewimage = Uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext ();    Cgimageref imageref = viewimage.cgimage; CGRect rect = CGRectMake (166, 211, 426, 320);//Here you can set the desired area CGRect rect = _videorect;//Here you can set the desired area Cgimageref I    Magerefrect =cgimagecreatewithimageinrect (Imageref, rect);    UIImage *sendimage = [[UIImage alloc] initwithcgimage:imagerefrect]; Uiimagewritetosavedphotosalbum (sendimage, nil, nil, nil);//save picture to Photo gallery NSData *imageviewdata = uiimagepngrepresentation (        Sendimage);    Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);    NSString *documentsdirectory = [Paths objectatindex:0];    NSString *picturename= [NSString stringwithformat:@ "Screenshow_%d.png", 2]; NSString *savedimagepath = [Documentsdirectory strinGbyappendingpathcomponent:picturename];        NSLog (@ "screenshot path printing:%@", Savedimagepath); [Imageviewdata Writetofile:savedimagepath atomically:yes];//Save the photo to the sandbox directory Cgimagerelease (imagerefrect);

  

Video One

Asset there are two methods, the following method is one, compared to (Video II) that method, this one is much faster
But I can not control him a lot of details, so recommended to use (video three)

Nsurl *url = [[Nsurl alloc] initwithstring:[nsstring stringWithFormat:@"%@", URLString]];//Initfileurlwithpath:videourl] autorelease];Avurlasset *urlasset =[Avurlasset Urlassetwithurl:url Options:nil]; Avassetimagegenerator*generator =[Avassetimagegenerator Assetimagegeneratorwithasset:urlasset]; Generator.appliespreferredtracktransform=NO; Generator.maximumsize=_videorect.size; Nserror*error =Nil;  for(floati =0; I < second; i + =0.1) {    //don't cmtime, just get to know.Cgimageref img = [Generator Copycgimageattime:cmtimemake (I,Ten) Actualtime:null error:&ERROR]; UIImage*image =[UIImage imagewithcgimage:img];    [Imagearray Addobject:image]; } NSLog (@"Iamgearray =%@", Imagearray); returnImagearray;

Video II

IOS 9.0 does not use the following methods, if a large number of suggestions do not use this method, very slow, but a bit is the time in the Mpmovie is our usual time 1 seconds corresponding to the video in the 1th second, two seconds corresponding to the video 2nd seconds ..., and a few lines of code is done

Nsmutablearray *imagearray = [Nsmutablearray array]; Mpmovieplayerviewcontroller * Mpviemcontroller =[[mpmovieplayerviewcontroller alloc] initwithcontenturl:urlstring]; MPMoviePlayerController *mp=[mpviemcontroller movieplayer];for (float i = 0; i < second; i + = 0.1) {    UIImage *thumb IMAGE=[MP thumbnailimageattime:i timeoption:mpmovietimeoptionexact];    Mpmovietimeoptionexact intercepts each frame of the picture, Mpmovietimeoptionnearestkeyframe keyframe    nsdata *imagedata = Uiimagepngrepresentation (thumbimage);    UIImage *image = [UIImage imagewithdata:imagedata];    [Imagearray addobject:image];} return imagearray;

  

Video Three

This is the fastest way to cut pictures, the fastest way in asset, using blocks to do, faster than the other thieves more

pragma mark---video capture-(void) Cutvideogetimagearraywithurl: (Nsurl *) urlstring Second: (float) second{_imagearr            ay = [Nsmutablearray array]; Avurlasset *urlasset = [Avurlasset urlassetwithurl:urlstring options:nil];////Get video length, Unit: SEC avassetimagegenerator *ge    Nerator = [Avassetimagegenerator assetimagegeneratorwithasset:urlasset];    Generator.appliespreferredtracktransform = YES;    Generator.maximumsize = _videorect.size;    The following two sentences are the key to intercept each frame Generator.requestedtimetolerancebefore = Kcmtimezero;        Generator.requestedtimetoleranceafter = Kcmtimezero;        Nsmutablearray *timearray = [Nsmutablearray array];    for (int i = 1; i < second * + 1; i++) {[Timearray addobject:[nsvalue valuewithcmtime:cmtimemake (i * 6, 60)]; }//Start [Generator Generatecgimagesasynchronouslyfortimes:timearray completionhandler:^ (Cmtime requestedtime, CGIm Ageref _nullable image, Cmtime actualtime, avassetimagegeneratorresult result, Nserror * _nullable error) {UIImage *imagesen = [UIImage imagewithcgimage:image];    [_imagearray Addobject:imagesen];    }]; }

  

Above.

IOS Video Capture

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.