Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 83 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. iOS: Memory leak in VideoToolBox while Creating Video file from list of Image
  2. (BOOL)appendPixelBuffer:(CVPixelBufferRef)pixelBuffer withPresentationTime:(CMTime)presentationTime
  3.    
  4. #import "ImageToVideo.h"
  5.  
  6. @implementation ImageToVideo
  7.  
  8. //global pixcel buffer.
  9. CVPixelBufferRef pxbuffer = NULL;
  10.  
  11. - (void) pixelBufferFromCGImage:(CGImageRef)image size:(CGSize)size
  12. {
  13.     NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
  14.                              [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
  15.                              [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
  16.                              nil];
  17.  
  18.     CVBufferRelease(pxbuffer);
  19.     CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, size.width,
  20.                                           size.height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options,
  21.                                           &pxbuffer);
  22.     options = nil;
  23.     [options release];
  24.  
  25.     status=status;//Added to make the stupid compiler not show a stupid warning.
  26.     NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
  27.  
  28.     CVPixelBufferLockBaseAddress(pxbuffer, 0);
  29.     void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
  30.     NSParameterAssert(pxdata != NULL);
  31.  
  32.     CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
  33.     CGContextRef context = CGBitmapContextCreate(pxdata, size.width,
  34.                                                  size.height, 8, 4*size.width, rgbColorSpace,
  35.                                                  kCGImageAlphaNoneSkipFirst);
  36.     NSParameterAssert(context);
  37.  
  38.     CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
  39.                                            CGImageGetHeight(image)), image);
  40.     CGColorSpaceRelease(rgbColorSpace);
  41.     CGContextRelease(context);
  42.  
  43.     CVPixelBufferUnlockBaseAddress(pxbuffer, 0);  
  44.  
  45. }
  46.  
  47.  
  48. - (void)writeImageAsMovietoPath:(NSString*)path size:(CGSize)size
  49. {
  50.     NSError *error = nil;
  51.     AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
  52.                                   [NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
  53.                                                               error:&error];
  54.     NSParameterAssert(videoWriter);
  55.  
  56.     NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
  57.                                    AVVideoCodecH264, AVVideoCodecKey,
  58.                                    [NSNumber numberWithInt:size.width], AVVideoWidthKey,
  59.                                    [NSNumber numberWithInt:size.height], AVVideoHeightKey,
  60.                                    nil];
  61.     AVAssetWriterInput* writerInput = [[AVAssetWriterInput
  62.                                         assetWriterInputWithMediaType:AVMediaTypeVideo
  63.                                         outputSettings:videoSettings] retain];
  64.  
  65.     AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
  66.                                                      assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
  67.                                                      sourcePixelBufferAttributes:nil];
  68.     NSParameterAssert(writerInput);
  69.     NSParameterAssert([videoWriter canAddInput:writerInput]);
  70.     [videoWriter addInput:writerInput];
  71.  
  72.     NSMutableArray *photoImages = [[[NSMutableArray alloc] init] autorelease];
  73.  
  74.     //Creating a image Array
  75.     for (int i = 0; i = [photoImages count])
  76.             {
  77.                 CVBufferRelease(pxbuffer);
  78.                 pxbuffer = NULL;
  79.  
  80.             }
  81.             else
  82.             {
  83.                 //creating the pixcel buffer.
  84.                 [self pixelBufferFromCGImage:[[photoImages objectAtIndex:i] CGImage] size:CGSizeMake(640, 980)];
  85.             }          
  86.  
  87.  
  88.             if (pxbuffer)
  89.             {
  90.                 // append buffer
  91.                 [adaptor appendPixelBuffer:pxbuffer withPresentationTime:presentTime];
  92.                 CVBufferRelease(pxbuffer);
  93.                 pxbuffer = NULL;
  94.                 i++;
  95.             }
  96.             else
  97.             {
  98.                 //Finish the session:
  99.                 [writerInput markAsFinished];
  100.                 [videoWriter finishWriting];                
  101.  
  102.                 CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
  103.                 [videoWriter release];
  104.                 [writerInput release];
  105.                 NSLog (@"Done");
  106.                 break;
  107.             }
  108.         }
  109.     }
  110.  
  111.     //release the photoImage array.
  112.     [photoImages removeAllObjects];
  113. }
  114.  
  115. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  116. {
  117.     // Unable to save the image  
  118.     if (error)
  119.     {
  120.         UIAlertView *alert;
  121.         alert = [[UIAlertView alloc] initWithTitle:@"Error"
  122.                                            message:@"Unable to save image to Photo Album."
  123.                                           delegate:self cancelButtonTitle:@"Ok"
  124.                                  otherButtonTitles:nil];
  125.         [alert show];
  126.         [alert release];
  127.     }
  128. }
  129.  
  130.  
  131.  
  132. - (void) main
  133. {
  134.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  135.     NSLog(@"Operation Started");
  136.  
  137.     NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/movie.mp4"]];  
  138.     CGSize size = CGSizeMake(640, 960);
  139.  
  140.     [self writeImageAsMovietoPath:path size:size] ;
  141.  
  142.     [pool drain];
  143. }
  144.  
  145.  
  146. @end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top