1. - (void) writeImagesAsMovie:(NSArray *)array toPath:(NSString*)path {
2.
3. NSString *documents = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
4. documents = [documents stringByAppendingPathComponent:currentWorkspace];
5.
6. //NSLog(path);
7. NSString *filename = [documents stringByAppendingPathComponent:[array objectAtIndex:0]];
8. UIImage *first = [UIImage imageWithContentsOfFile:filename];
9.
10. CGSize frameSize = first.size;
11.
12. NSError *error = nil;
13. AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
14. [NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
15. error:&error];
16.
17. if(error) {
18. NSLog(@"error creating AssetWriter: %@",[error description]);
19. }
20. NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
21. AVVideoCodecH264, AVVideoCodecKey,
22. [NSNumber numberWithInt:frameSize.width], AVVideoWidthKey,
23. [NSNumber numberWithInt:frameSize.height], AVVideoHeightKey,
24. nil];
25.
26. AVAssetWriterInput* writerInput = [[AVAssetWriterInput
27. assetWriterInputWithMediaType:AVMediaTypeVideo
28. outputSettings:videoSettings] retain];
29.
30. NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
31. [attributes setObject:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32ARGB] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];
32. [attributes setObject:[NSNumber numberWithUnsignedInt:frameSize.width] forKey:(NSString*)kCVPixelBufferWidthKey];
33. [attributes setObject:[NSNumber numberWithUnsignedInt:frameSize.height] forKey:(NSString*)kCVPixelBufferHeightKey];
34.
35. AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
36. assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
37. sourcePixelBufferAttributes:attributes];
38.
39. [videoWriter addInput:writerInput];
40.
41. // fixes all errors
42. writerInput.expectsMediaDataInRealTime = YES;
43.
44. //Start a session:
45. BOOL start = [videoWriter startWriting];
46. NSLog(@"Session started? %d", start);
47. [videoWriter startSessionAtSourceTime:kCMTimeZero];
48.
1. CVPixelBufferRef buffer = NULL;
2. buffer = [self pixelBufferFromCGImage:[first CGImage]];
3. BOOL result = [adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];
4.
5. if (result == NO) //failes on 3GS, but works on iphone 4
6. NSLog(@"failed to append buffer");
7.
8. if(buffer)
9. CVBufferRelease(buffer);
10.
11. [NSThread sleepForTimeInterval:0.05];
12.
13. int reverseSort = NO;
14. NSArray *newArray = [array sortedArrayUsingFunction:sort context:&reverseSort];
15.
16. delta = 1.0/[newArray count];
17.
18. int fps = (int)fpsSlider.value;
19.
20. int i = 0;
21. for (NSString *filename in newArray)
22. {
23. if (adaptor.assetWriterInput.readyForMoreMediaData)
24. {
25.
26. i++;
27. NSLog(@"inside for loop %d %@ ",i, filename);
28. CMTime frameTime = CMTimeMake(1, fps);
29. CMTime lastTime=CMTimeMake(i, fps);
30. CMTime presentTime=CMTimeAdd(lastTime, frameTime);
31.
32. NSString *filePath = [documents stringByAppendingPathComponent:filename];
33.
34. UIImage *imgFrame = [UIImage imageWithContentsOfFile:filePath] ;
35. buffer = [self pixelBufferFromCGImage:[imgFrame CGImage]];
36. BOOL result = [adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];
37.
38. if (result == NO) //failes on 3GS, but works on iphone 4
39. {
40. NSLog(@"failed to append buffer");
41. NSLog(@"The error is %@", [videoWriter error]);
42. }
43. if(buffer)
44. CVBufferRelease(buffer);
45. [NSThread sleepForTimeInterval:0.05];
46. }
47. else
48. {
49. NSLog(@"error");
50. i--;
51. }
52. [NSThread sleepForTimeInterval:0.02];
53. }
54.
55. //Finish the session:
56. [writerInput markAsFinished];
57. [videoWriter finishWriting];
58. CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
59. [videoWriter release];
60. [writerInput release];
61. }
1. - (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image
2. {
3. NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
4. [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
5. [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
6. nil];
7. CVPixelBufferRef pxbuffer = NULL;
8.
9. CVPixelBufferCreate(kCFAllocatorDefault, CGImageGetWidth(image),
10. CGImageGetHeight(image), kCVPixelFormatType_32ARGB, (CFDictionaryRef) options,
11. &pxbuffer);
12.
13. CVPixelBufferLockBaseAddress(pxbuffer, 0);
14. void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
15.
16. CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
17. CGContextRef context = CGBitmapContextCreate(pxdata, CGImageGetWidth(image),
18. CGImageGetHeight(image), 8, 4*CGImageGetWidth(image), rgbColorSpace,
19. kCGImageAlphaNoneSkipFirst);
20.
21. CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));
22.
23. CGAffineTransform flipVertical = CGAffineTransformMake(
24. 1, 0, 0, -1, 0, CGImageGetHeight(image)
25. );
26. CGContextConcatCTM(context, flipVertical);
27.
28. CGAffineTransform flipHorizontal = CGAffineTransformMake(
29. -1.0, 0.0, 0.0, 1.0, CGImageGetWidth(image), 0.0
30. );
31.
32. CGContextConcatCTM(context, flipHorizontal);
33.
34. CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
35. CGImageGetHeight(image)), image);
36. CGColorSpaceRelease(rgbColorSpace);
37. CGContextRelease(context);
38.
39. CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
40.
41. return pxbuffer;
42. }
2.
3. NSString *documents = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
4. documents = [documents stringByAppendingPathComponent:currentWorkspace];
5.
6. //NSLog(path);
7. NSString *filename = [documents stringByAppendingPathComponent:[array objectAtIndex:0]];
8. UIImage *first = [UIImage imageWithContentsOfFile:filename];
9.
10. CGSize frameSize = first.size;
11.
12. NSError *error = nil;
13. AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
14. [NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
15. error:&error];
16.
17. if(error) {
18. NSLog(@"error creating AssetWriter: %@",[error description]);
19. }
20. NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
21. AVVideoCodecH264, AVVideoCodecKey,
22. [NSNumber numberWithInt:frameSize.width], AVVideoWidthKey,
23. [NSNumber numberWithInt:frameSize.height], AVVideoHeightKey,
24. nil];
25.
26. AVAssetWriterInput* writerInput = [[AVAssetWriterInput
27. assetWriterInputWithMediaType:AVMediaTypeVideo
28. outputSettings:videoSettings] retain];
29.
30. NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
31. [attributes setObject:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32ARGB] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];
32. [attributes setObject:[NSNumber numberWithUnsignedInt:frameSize.width] forKey:(NSString*)kCVPixelBufferWidthKey];
33. [attributes setObject:[NSNumber numberWithUnsignedInt:frameSize.height] forKey:(NSString*)kCVPixelBufferHeightKey];
34.
35. AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
36. assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
37. sourcePixelBufferAttributes:attributes];
38.
39. [videoWriter addInput:writerInput];
40.
41. // fixes all errors
42. writerInput.expectsMediaDataInRealTime = YES;
43.
44. //Start a session:
45. BOOL start = [videoWriter startWriting];
46. NSLog(@"Session started? %d", start);
47. [videoWriter startSessionAtSourceTime:kCMTimeZero];
48.
1. CVPixelBufferRef buffer = NULL;
2. buffer = [self pixelBufferFromCGImage:[first CGImage]];
3. BOOL result = [adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];
4.
5. if (result == NO) //failes on 3GS, but works on iphone 4
6. NSLog(@"failed to append buffer");
7.
8. if(buffer)
9. CVBufferRelease(buffer);
10.
11. [NSThread sleepForTimeInterval:0.05];
12.
13. int reverseSort = NO;
14. NSArray *newArray = [array sortedArrayUsingFunction:sort context:&reverseSort];
15.
16. delta = 1.0/[newArray count];
17.
18. int fps = (int)fpsSlider.value;
19.
20. int i = 0;
21. for (NSString *filename in newArray)
22. {
23. if (adaptor.assetWriterInput.readyForMoreMediaData)
24. {
25.
26. i++;
27. NSLog(@"inside for loop %d %@ ",i, filename);
28. CMTime frameTime = CMTimeMake(1, fps);
29. CMTime lastTime=CMTimeMake(i, fps);
30. CMTime presentTime=CMTimeAdd(lastTime, frameTime);
31.
32. NSString *filePath = [documents stringByAppendingPathComponent:filename];
33.
34. UIImage *imgFrame = [UIImage imageWithContentsOfFile:filePath] ;
35. buffer = [self pixelBufferFromCGImage:[imgFrame CGImage]];
36. BOOL result = [adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];
37.
38. if (result == NO) //failes on 3GS, but works on iphone 4
39. {
40. NSLog(@"failed to append buffer");
41. NSLog(@"The error is %@", [videoWriter error]);
42. }
43. if(buffer)
44. CVBufferRelease(buffer);
45. [NSThread sleepForTimeInterval:0.05];
46. }
47. else
48. {
49. NSLog(@"error");
50. i--;
51. }
52. [NSThread sleepForTimeInterval:0.02];
53. }
54.
55. //Finish the session:
56. [writerInput markAsFinished];
57. [videoWriter finishWriting];
58. CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
59. [videoWriter release];
60. [writerInput release];
61. }
1. - (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image
2. {
3. NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
4. [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
5. [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
6. nil];
7. CVPixelBufferRef pxbuffer = NULL;
8.
9. CVPixelBufferCreate(kCFAllocatorDefault, CGImageGetWidth(image),
10. CGImageGetHeight(image), kCVPixelFormatType_32ARGB, (CFDictionaryRef) options,
11. &pxbuffer);
12.
13. CVPixelBufferLockBaseAddress(pxbuffer, 0);
14. void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
15.
16. CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
17. CGContextRef context = CGBitmapContextCreate(pxdata, CGImageGetWidth(image),
18. CGImageGetHeight(image), 8, 4*CGImageGetWidth(image), rgbColorSpace,
19. kCGImageAlphaNoneSkipFirst);
20.
21. CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));
22.
23. CGAffineTransform flipVertical = CGAffineTransformMake(
24. 1, 0, 0, -1, 0, CGImageGetHeight(image)
25. );
26. CGContextConcatCTM(context, flipVertical);
27.
28. CGAffineTransform flipHorizontal = CGAffineTransformMake(
29. -1.0, 0.0, 0.0, 1.0, CGImageGetWidth(image), 0.0
30. );
31.
32. CGContextConcatCTM(context, flipHorizontal);
33.
34. CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
35. CGImageGetHeight(image)), image);
36. CGColorSpaceRelease(rgbColorSpace);
37. CGContextRelease(context);
38.
39. CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
40.
41. return pxbuffer;
42. }