1
    #import "ViewController.h"
    #import "AVFoundation/AVAssetWriter.h"
    #import "AVFoundation/AVAssetWriterInput.h"
    #import "AVFoundation/AVMediaFormat.h"
    #import "AVFoundation/AVVideoSettings.h"


    @implementation ViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];

         NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDirectory = [documentDirectories objectAtIndex: 0];

       // NSLog(@"Where it is %@ \n",documentDirectory);
        image1.image = [UIImage imageNamed:@"images1.jpg"];
         CGSize sizeOfImage = image1.image.size;
        // printf("Size of Image width = %f  height = %f\n", sizeOfImage.width,sizeOfImage.height);
        [self writeImageAsMovie:(UIImage*)image1.image toPath:(NSString*)documentDirectory size:(CGSize)sizeOfImage duration:6];

    }


    - (void)writeImageAsMovie:(UIImage*)image toPath:(NSString*)path size:(CGSize)size duration:(int)duration 
    {
        NSError *error = nil;

        AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                                      [NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
                                                                  error:&error];
        NSParameterAssert(videoWriter);


        NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                       AVVideoCodecH264, AVVideoCodecKey,
                                       [NSNumber numberWithInt:size.width], AVVideoWidthKey,
                                       [NSNumber numberWithInt:size.height], AVVideoHeightKey,
                                       nil];


        AVAssetWriterInput *writerInput = [AVAssetWriterInput
                                           assetWriterInputWithMediaType:AVMediaTypeVideo
                                           outputSettings:videoSettings];




        AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                         assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
                                                         sourcePixelBufferAttributes:nil];


        NSParameterAssert(writerInput);
        NSParameterAssert([videoWriter canAddInput:writerInput]);
        [videoWriter addInput:writerInput];

        //Start a session:
        [videoWriter startWriting];

        [videoWriter startSessionAtSourceTime:kCMTimeZero];

        //Write samples:
        //CVPixelBufferRef Utils;
        CVPixelBufferRef buffer = [self newPixelBufferFromCGImage:image.CGImage size:size];
        [adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];



        [adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(duration-1, 2)];


        //Finish the session:
        [writerInput markAsFinished];
        [videoWriter endSessionAtSourceTime:CMTimeMake(duration, 2)];



       [videoWriter finishWriting]; 


    } 

-(CVPixelBufferRef) newPixelBufferFromCGImage:(CGImageRef)image size:(CGSize )frameSize
{
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                             [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                             nil];


    CVPixelBufferRef pxbuffer = NULL;


    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, frameSize.width, frameSize.height, kCVPixelFormatType_32ARGB,(__bridge CFDictionaryRef)options,&pxbuffer);

    NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);

    status = CVPixelBufferLockBaseAddress(pxbuffer, 0);
    void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
    NSParameterAssert(pxdata != NULL);

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pxdata, frameSize.width,
                                                 frameSize.height, 8, 4*frameSize.width, rgbColorSpace, 
                                                 kCGImageAlphaNoneSkipFirst);
    NSParameterAssert(context);
    CGAffineTransform frameTransform;
    CGContextConcatCTM(context, frameTransform);
    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), 
                                           CGImageGetHeight(image)), image);
    CGColorSpaceRelease(rgbColorSpace);
    CGContextRelease(context);

    CVPixelBufferUnlockBaseAddress(pxbuffer, 0);


    return pxbuffer;
} 


@end

I am new on iOS and i have seen a code which display video from images in stack overflow. I run the code and it didn't show any errors but i didn't get required output. I think the error is last line of this function

-(void)writeImageAsMovie:(UIImage*)image toPath:(NSString*)path size:(CGSize)size duration:(int)duration) that is [videoWriter finishWriting]

Please help me.

Max
  • 16,679
  • 4
  • 44
  • 57
Prasad G
  • 6,702
  • 7
  • 42
  • 65

1 Answers1

0

If you have an array of images and just to play these images as an video, you can use an UIImageView and update the images in the array to the image view using a for loop.

If you have images in the array. Like,

for (int i = 0; i < [array count]; i++)
{    
    imageview.image = (UIImage *)[array objectAtIndex:i];
}
Basheer
  • 1,207
  • 9
  • 10
  • Well. But it's not my answer. My error is in "[videoWriter finishWriting];". In this line, finishWriting is not successful. That's why i didn't get output. Please check my code and correct it. – Prasad G Dec 26 '11 at 11:37
  • I suggested you to use this approach instead of the above one. Its quite straight forward and simple to implement. – Basheer Dec 26 '11 at 12:51