How to convert image sequences (PNG) into Animation File (gif/mov/others) (iPhone)?

I have a sequence of images (PNG format) in my iphone app. It is saved in the app's sandbox doc folder. How do I create animation file from these images?

I would prefer animated gif, but other types like video (mov/avi/etc) is ok too. I don't need audio. Just images. Thanks.

Answers


Check out this tutorial on iCodeBlog (it shows how to animate PNG files): iPhone Programming Tutorial: Animating a Game Sprite | iCodeBlog


This works for me

-(void)viewDidLoad {
    [super viewDidLoad];
    // here u can replace with  your images 
    imagesArray=[[NSMutableArray alloc]initWithObjects:@"BALL_EASY.png",@"Rat.png", @"HARD_CARD.png", @"EASY_CARD.png", @"MEDIUM_CARD.png", @"BattleMapSplashScreen.png", @"MENU_Bee.png",  nil];

    NSString *fileName =@"myMovie.txt";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    NSString   *filePath = [documentsDir stringByAppendingPathComponent:fileName];

    [self writeImageAsMovie:imagesArray  toPath:filePath  size:CGSizeMake(320.0, 440.0)];

}

-(CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image 
    {
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES],   kCVPixelBufferCGImageCompatibilityKey,
                             [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                             nil];
    CVPixelBufferRef pxbuffer = NULL;
    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, self.view.frame.size.width,
                                          self.view.frame.size.height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options, 
                                          &pxbuffer);
    NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);

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

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pxdata,self.view.frame.size.width,
                                                 self.view.frame.size.height, 8, 4*self.view.frame.size.width, rgbColorSpace, 
                                                 kCGImageAlphaNoneSkipFirst);
    NSParameterAssert(context);
    CGContextConcatCTM(context,  self.view.transform);
    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), 
                                           CGImageGetHeight(image)), image);
    CGColorSpaceRelease(rgbColorSpace);
    CGContextRelease(context);

    CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

    return pxbuffer;
}


-(void)writeImageAsMovie:(NSArray *)array toPath:(NSString*)path size:(CGSize)size
    {
    NSMutableDictionary *attributes = [[NSMutableDictionary alloc]init];
    [attributes setObject:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32ARGB] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];
    [attributes setObject:[NSNumber numberWithUnsignedInt:320] forKey:(NSString*)kCVPixelBufferWidthKey];
    [attributes setObject:[NSNumber numberWithUnsignedInt:416] forKey:(NSString*)kCVPixelBufferHeightKey];

    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] retain];

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

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


    //Start a session:
    [videoWriter startWriting];
    [videoWriter startSessionAtSourceTime:kCMTimeZero];

    CVPixelBufferRef buffer = NULL;
    buffer = [self pixelBufferFromCGImage:[[array objectAtIndex:0] CGImage]];
    [adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];

    //Write samples:
    for (int i = 0;i<[array count]; i++)
    {
        if([writerInput isReadyForMoreMediaData])
        {
            NSLog(@"inside for loop %d",i);
            CMTime frameTime = CMTimeMake(1, 20);

            CMTime lastTime=CMTimeMake(i, 20); //i is from 0 to 19 of the loop above

            CMTime presentTime=CMTimeAdd(lastTime, frameTime);

            buffer = [self pixelBufferFromCGImage:[[array objectAtIndex:i] CGImage]];

            [adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];

            if(buffer)
                CVBufferRelease(buffer);
        }
        else
        {
            NSLog(@"error");
            i--;
        }

    }

    //Finish the session:
    [writerInput markAsFinished];
    [videoWriter finishWriting];

    NSURL *pathURL = [NSURL fileURLWithPath:path];

    AVURLAsset *url = [[AVURLAsset alloc] initWithURL:pathURL options:nil];


    [url release];
    CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
    [videoWriter release];
    [writerInput release];

}

Need Your Help

Custom JComponent not displaying in Custom JPanel

java jpanel java-2d jcomponent

I've tried the add() method but nothing is displayed when I try to add Test to GraphicsTest. How should I be adding it? Can someone show me? I've included the code I'm using.

Multiple Image Sliders 1 single Page with example

javascript html html5 css3

I'm trying to build an multiple image slider and the problem is I cant show booth sliders at same time.