Didn’t find any video file (created from images) in document folder

I am creating video from bunch of images. I have tried this :

@interface ViewController ()
{
      NSMutableArray *arrFrames;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

  NSError *error;
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
  NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];
  NSLog(@"Path - %@", dataPath);

  if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

  arrFrames = [NSMutableArray new];

  for (int i=0; i<236; i++) {

       NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"frame_%d_delay-0.03s", i] ofType:@"png"];
       UIImage *img = [UIImage imageWithContentsOfFile:path];
    [arrFrames addObject:img];
  }

[self writeImageAsMovie:nil toPath:dataPath size:CGSizeMake([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height) duration:5];
}

- (void)writeImageAsMovie:(UIImage *)image1 toPath:(NSString *)path size:(CGSize)size duration:(int)duration
{
  NSError *error = nil;
  AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:path] fileType:AVFileTypeMPEG4 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];

  for (UIImage *image in arrFrames) {

    //Write samples:
    CVPixelBufferRef buffer = [self pixelBufferFromCGImage: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) pixelBufferFromCGImage:(CGImageRef)image size:(CGSize)size
{
  NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil];

  CVPixelBufferRef pxbuffer = NULL;
  CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, size.width, size.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options, &pxbuffer);
  status=status; //Added to make the stupid compiler not show a stupid warning.
  NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);

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

  CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
  CGContextRef context = CGBitmapContextCreate(pxdata, size.width, size.height, 8, 4*size.width, rgbColorSpace, kCGImageAlphaNoneSkipFirst);
  NSParameterAssert(context);

  //CGContextTranslateCTM(context, 0, CGImageGetHeight(image));
  //CGContextScaleCTM(context, 1.0, -1.0);//Flip vertically to account for different origin

  CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
  CGColorSpaceRelease(rgbColorSpace);
  CGContextRelease(context);
  CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
  return pxbuffer;
}

But when I go to Documents —> MyFolder there is nothing to see. And in Library —> Caches —> Snapshots —> com.VideoDemo (Library is parallel to Documents), at there two .ktk files are created. Which named as :

  • 63D3DF39-96D3-4DCC-B1D6-A4E85E3FD6DD@2x.ktx
  • A3E1A526-F573-4714-88CC-5483C486C59E@2x.ktx

But I can’t open or play these.

My question is How can I create video from bunch of images.

@vimipatel. Thanks very much for your question, and my apologies for the delayed response.

If you need help with such large blocks of code, the best way to get a solution would be to go to the site: www.stackoverflow.com. I can almost guarantee you that you will get a solution within 24 hours. :slight_smile:

As for the specifics for what you’re asking, I would suggest personally to generate a video of the images out side of the app, and simply utilize the video file inside the app itself, unless there is a reason as to why you wish to generate the animation inside the app. If this is necessary, I would also recommend you go through some of the animation tutorials on our site to see how similar effects are accomplished, and try those approaches.

I hope this helps!

All the best!