IOS多张图片合成一个视频

  1. - (void)viewDidLoad  
  2. {  
  3.    [superviewDidLoad];  
  4.     //Do any additional setup after loading the view.  
  5.    imageArr =[[NSMutableArrayalloc]initWithObjects:  
  6.              [UIImageimageNamed:@"1"],[UIImageimageNamed:@"2.jpg"],[UIImageimageNamed:@"3.jpg"],[UIImageimageNamed:@"4.jpg"],[UIImageimageNamed:@"5.jpg"],[UIImageimageNamed:@"6.jpg"],[UIImageimageNamed:@"7.jpg"],[UIImageimageNamed:@"8.jpg"],[UIImageimageNamed:@"9.jpg"],[UIImageimageNamed:@"10.jpg"],[UIImageimageNamed:@"11.jpg"],[UIImageimageNamed:@"12.jpg"],[UIImageimageNamed:@"13.jpg"],[UIImageimageNamed:@"14.jpg"],[UIImageimageNamed:@"15"],[UIImageimageNamed:@"16"],[UIImageimageNamed:@"17"],[UIImageimageNamed:@"18"],[UIImageimageNamed:@"19"],[UIImageimageNamed:@"20"],[UIImageimageNamed:@"21"],[UIImageimageNamed:@"22"],[UIImageimageNamed:@"23"],[UIImageimageNamed:@"24"],[UIImageimageNamed:@"25"],[UIImageimageNamed:@"26"],[UIImageimageNamed:@"27"],[UIImageimageNamed:@"28"],[UIImageimageNamed:@"29"],[UIImageimageNamed:@"30"],[UIImageimageNamed:@"31"],nil];  
  7.      
  8.    UIButton * button =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];  
  9.     [buttonsetFrame:CGRectMake(100,100100,100)];  
  10.    [buttonsetTitle:@"合成"forState:UIControlStateNormal];  
  11.    [buttonaddTarget:selfaction:@selector(testCompressionSession)forControlEvents:UIControlEventTouchUpInside];  
  12.    [self.viewaddSubview:button];  
  13.      
  14.    UIButton * button1 =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];  
  15.     [button1setFrame:CGRectMake(100,200100,100)];  
  16.    [button1setTitle:@"播放"forState:UIControlStateNormal];  
  17.    [button1addTarget:selfaction:@selector(playAction)forControlEvents:UIControlEventTouchUpInside];  
  18.    [self.viewaddSubview:button1];  
  19. }  
  20.   
  21. -(void)testCompressionSession  
  22. {  
  23.    NSLog(@"开始");  
  24.    //NSString *moviePath = [[NSBundle mainBundle]pathForResource:@"Movie" ofType:@"mov"];  
  25.    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);  
  26.    NSString *moviePath =[[pathsobjectAtIndex:0]stringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.mov",@"test"]];  
  27.    self.theVideoPath=moviePath;  
  28.    CGSize size =CGSizeMake(320,400);//定义视频的大小  
  29.      
  30. //    [selfwriteImages:imageArr ToMovieAtPath:moviePath withSize:sizeinDuration:4 byFPS:30];//第2中方法  
  31.   
  32.    NSError *error =nil;  
  33.      
  34.    unlink([moviePathUTF8String]);  
  35.    NSLog(@"path->%@",moviePath);  
  36.    //—-initialize compression engine  
  37.    AVAssetWriter *videoWriter =[[AVAssetWriteralloc]initWithURL:[NSURLfileURLWithPath:moviePath]  
  38.                                                  fileType:AVFileTypeQuickTimeMovie  
  39.                                                    error:&error];  
  40.    NSParameterAssert(videoWriter);  
  41.    if(error)  
  42.       NSLog(@"error =%@", [errorlocalizedDescription]);  
  43.      
  44.    NSDictionary *videoSettings =[NSDictionarydictionaryWithObjectsAndKeys:AVVideoCodecH264,AVVideoCodecKey,  
  45.                              [NSNumbernumberWithInt:size.width],AVVideoWidthKey,  
  46.                              [NSNumbernumberWithInt:size.height],AVVideoHeightKey,nil];  
  47.    AVAssetWriterInput *writerInput =[AVAssetWriterInputassetWriterInputWithMediaType:AVMediaTypeVideooutputSettings:videoSettings];  
  48.      
  49.    NSDictionary*sourcePixelBufferAttributesDictionary =[NSDictionarydictionaryWithObjectsAndKeys:[NSNumbernumberWithInt:kCVPixelFormatType_32ARGB],kCVPixelBufferPixelFormatTypeKey,nil];  
  50.      
  51.    AVAssetWriterInputPixelBufferAdaptor *adaptor =[AVAssetWriterInputPixelBufferAdaptor  
  52.                                             assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInputsourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];  
  53.    NSParameterAssert(writerInput);  
  54.    NSParameterAssert([videoWritercanAddInput:writerInput]);  
  55.      
  56.    if ([videoWritercanAddInput:writerInput])  
  57.       NSLog(@"11111");  
  58.    else  
  59.       NSLog(@"22222");  
  60.      
  61.     [videoWriteraddInput:writerInput];  
  62.      
  63.     [videoWriterstartWriting];  
  64.    [videoWriterstartSessionAtSourceTime:kCMTimeZero];  
  65.      
  66.    //合成多张图片为一个视频文件  
  67.    dispatch_queue_t dispatchQueue =dispatch_queue_create("mediaInputQueue",NULL);  
  68.    int __block frame =0;  
  69.      
  70.    [writerInputrequestMediaDataWhenReadyOnQueue:dispatchQueueusingBlock:^{  
  71.        while([writerInputisReadyForMoreMediaData])  
  72.        {  
  73.           if(++frame >=[imageArrcount]*10)  
  74.            {  
  75.               [writerInputmarkAsFinished];  
  76.               [videoWriterfinishWriting];  
  77. //              [videoWriterfinishWritingWithCompletionHandler:nil];  
  78.              break;  
  79.            }  
  80.             
  81.           CVPixelBufferRef buffer =NULL;  
  82.             
  83.           int idx =frame/10;  
  84.           NSLog(@"idx==%d",idx);  
  85.             
  86.            buffer =(CVPixelBufferRef)[selfpixelBufferFromCGImage:[[imageArrobjectAtIndex:idx]CGImage]size:size];  
  87.             
  88.           if (buffer)  
  89.            {  
  90.              if(![adaptorappendPixelBuffer:bufferwithPresentationTime:CMTimeMake(frame,10)])  
  91.                 NSLog(@"FAIL");  
  92.              else  
  93.                 NSLog(@"OK");  
  94.                 CFRelease(buffer);  
  95.            }  
  96.        }  
  97.     }];  
  98. }  
  99.   
  100. - (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)imagesize:(CGSize)size  
  101. {  
  102.    NSDictionary *options =[NSDictionarydictionaryWithObjectsAndKeys:  
  103.                         [NSNumbernumberWithBool:YES],kCVPixelBufferCGImageCompatibilityKey,  
  104.                         [NSNumbernumberWithBool:YES],kCVPixelBufferCGBitmapContextCompatibilityKey,nil];  
  105.    CVPixelBufferRef pxbuffer =NULL;  
  106.    CVReturn status =CVPixelBufferCreate(kCFAllocatorDefault,size.width,size.height,kCVPixelFormatType_32ARGB,(__bridgeCFDictionaryRef) options,&pxbuffer);  
  107.      
  108.    NSParameterAssert(status ==kCVReturnSuccess && pxbuffer !=NULL);  
  109.      
  110.    CVPixelBufferLockBaseAddress(pxbuffer,0);  
  111.    voidvoid *pxdata =CVPixelBufferGetBaseAddress(pxbuffer);  
  112.    NSParameterAssert(pxdata !=NULL);  
  113.      
  114.    CGColorSpaceRef rgbColorSpace=CGColorSpaceCreateDeviceRGB();  
  115.    CGContextRef context =CGBitmapContextCreate(pxdata,size.width,size.height,8,4*size.width,rgbColorSpace,kCGImageAlphaPremultipliedFirst);  
  116.    NSParameterAssert(context);  
  117.      
  118.    CGContextDrawImage(context,CGRectMake(0,0,CGImageGetWidth(image),CGImageGetHeight(image)), image);  
  119.      
  120.    CGColorSpaceRelease(rgbColorSpace);  
  121.    CGContextRelease(context);   
  122.      
  123.    CVPixelBufferUnlockBaseAddress(pxbuffer,0);   
  124.      
  125.    return pxbuffer;  
  126. }  
  127.   
  128. -(void)playAction  
  129. {  
  130.    MPMoviePlayerViewController *theMovie =[[MPMoviePlayerViewControlleralloc]initWithContentURL:[NSURLfileURLWithPath:self.theVideoPath]];  
  131.    [selfpresentMoviePlayerViewControllerAnimated:theMovie];  
  132.    theMovie.moviePlayer.movieSourceType=MPMovieSourceTypeFile;  
  133.    [theMovie.moviePlayerplay];  
  134. }  
  135.   
  136.   
  137. //第二种方式  
  138. - (void)writeImages:(NSArray *)imagesArrayToMovieAtPath:(NSString *) pathwithSize:(CGSize) size  
  139.         inDuration:(float)durationbyFPS:(int32_t)fps{  
  140.    //Wire the writer:  
  141.    NSError *error =nil;  
  142.    AVAssetWriter *videoWriter =[[AVAssetWriteralloc]initWithURL:[NSURLfileURLWithPath:path]  
  143.                                                   fileType:AVFileTypeQuickTimeMovie  
  144.                                                     error:&error];  
  145.    NSParameterAssert(videoWriter);  
  146.      
  147.    NSDictionary *videoSettings =[NSDictionarydictionaryWithObjectsAndKeys:  
  148.                              AVVideoCodecH264,AVVideoCodecKey,  
  149.                              [NSNumbernumberWithInt:size.width],AVVideoWidthKey,  
  150.                              [NSNumbernumberWithInt:size.height],AVVideoHeightKey,  
  151.                              nil nil];  
  152.      
  153.    AVAssetWriterInput* videoWriterInput =[AVAssetWriterInput  
  154.                                      assetWriterInputWithMediaType:AVMediaTypeVideo  
  155.                                      outputSettings:videoSettings];  
  156.      
  157.      
  158.    AVAssetWriterInputPixelBufferAdaptor *adaptor =[AVAssetWriterInputPixelBufferAdaptor  
  159.                                             assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput  
  160.                                             sourcePixelBufferAttributes:nil];  
  161.    NSParameterAssert(videoWriterInput);  
  162.    NSParameterAssert([videoWritercanAddInput:videoWriterInput]);  
  163.     [videoWriteraddInput:videoWriterInput];  
  164.      
  165.    //Start a session:  
  166.     [videoWriterstartWriting];  
  167.    [videoWriterstartSessionAtSourceTime:kCMTimeZero];  
  168.      
  169.    //Write some samples:  
  170.    CVPixelBufferRef buffer =NULL;  
  171.      
  172.    int frameCount =0;  
  173.      
  174.    int imagesCount = [imagesArraycount];  
  175.    float averageTime =duration/imagesCount;  
  176.    int averageFrame =(int)(averageTime * fps);  
  177.      
  178.    for(UIImage *img in imagesArray)  
  179.     {  
  180.        buffer=[selfpixelBufferFromCGImage:[imgCGImage]size:size];  
  181.        BOOL append_ok =NO;  
  182.        int j =0;  
  183.        while (!append_ok&& j <</b> 30)  
  184.        {  
  185.           if(adaptor.assetWriterInput.readyForMoreMediaData)  
  186.            {  
  187.              printf("appending %d attemp%d\n", frameCount, j);  
  188.                 
  189.              CMTime frameTime =CMTimeMake(frameCount,(int32_t)fps);  
  190.               floatframeSeconds =CMTimeGetSeconds(frameTime);  
  191.              NSLog(@"frameCount:%d,kRecordingFPS:%d,frameSeconds:%f",frameCount,fps,frameSeconds);  
  192.               append_ok = [adaptorappendPixelBuffer:bufferwithPresentationTime:frameTime];  
  193.                 
  194.              if(buffer)  
  195.                  [NSThreadsleepForTimeInterval:0.05];  
  196.            }  
  197.           else  
  198.            {  
  199.              printf("adaptor not ready %d,%d\n", frameCount, j);  
  200.              [NSThreadsleepForTimeInterval:0.1];  
  201.            }  
  202.           j++;  
  203.        }  
  204.        if (!append_ok){  
  205.          printf("error appendingimage %d times %d\n", frameCount, j);  
  206.        }  
  207.          
  208.        frameCount = frameCount + averageFrame;  
  209.     }  
  210.      
  211.    //Finish the session:  
  212.     [videoWriterInputmarkAsFinished];  
  213.     [videoWriterfinishWriting];  
  214.    NSLog(@"finishWriting");  
  215. }  
  216.   
  217. <span style="font-size:18px;color:#ff6600;">推荐一个别人写好的<a target=_blank href="https://github.com/HarrisonJackson/HJImagesToVideo">https://github.com/HarrisonJackson/HJImagesToVideo</a></span>  

你可能感兴趣的:(IOS多张图片合成一个视频)