开发者

iOS5 AVFoundation image to video

开发者 https://www.devze.com 2023-04-04 03:20 出处:网络
I\'m trying to create a video from a single image, and save it to my photos library, I\'ve been googling around for ages - and cannot find a solution.

I'm trying to create a video from a single image, and save it to my photos library, I've been googling around for ages - and cannot find a solution.

I have this code:

    @autoreleasepool {
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/movie2.mp4"]];

    UIImage *img = [UIImage imageWithData:[[self imageDataArrya]objectAtIndex:0]imageData];
    [self writeImageAsMovie:img toPath:path size:CGSizeMake(640, 960) duration:10];

    UISaveVideoAtPathToSavedPhotosAlbum (path,self, @selector(video:didFinishSavingWithError: contextInfo:), nil);
}

I call the above mentioned method in a background thread. This is the code for 'writeImageAsMovie':

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

NSDictionary *videoSettings = [NSDictionary dictionaryWithObject开发者_开发问答sAndKeys:
                               AVVideoCodecH264, AVVideoCodecKey,
                               [NSNumber numberWithInt:size.width], AVVideoWidthKey,
                               [NSNumber numberWithInt:size.height], AVVideoHeightKey,
                               nil];
[self setInput:[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo 
                                                  outputSettings:videoSettings]];

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

[videoWriter addInput:input];

[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];

CVPixelBufferRef buffer = [self pixelBufferFromCGImage:image.CGImage];
[adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];
[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(duration-1, 2)];

[input markAsFinished];
[videoWriter endSessionAtSourceTime:CMTimeMake(duration, 2)];
[videoWriter finishWriting];

}

The utility method for converting an Image to a CVPixelBufferRef:

- (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, 
                                      (__bridge CFDictionaryRef) options, 
                                      &pxbuffer);

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

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);
CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), 
                                       CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);

CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

return pxbuffer;
}

Now if I try to run the code from the Simulator, it gives me an error saying that the data is corrupt.

If I run it on my device, it saves a 2 second video to my photo library but its only green, my image isn't in there.

Any help will be appreciated :)


I totally got this working - sorry I didn't see your reply before today. This is what I used:

Create a Temp File

 NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/flipimator-tempfile.mp4"]];

//overwrites it if it already exists.
if([fileManager fileExistsAtPath:path]) 
    [fileManager removeItemAtPath:path error:NULL];

Call the export images method to save images to the temp file:

[self exportImages:frames 
         asVideoToPath:path 
         withFrameSize:imageSize 
       framesPerSecond:fps];

Save the temp file to the photo album:

UISaveVideoAtPathToSavedPhotosAlbum (path,self, @selector(video:didFinishSavingWithError: contextInfo:), nil);

- (void)video:(NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
    NSLog(@"Finished saving video with error: %@", error);
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Done"
                                                   message:@"Movie succesfully exported." 
                                          delegate:nil 
                                 cancelButtonTitle:@"OK" 
                                 otherButtonTitles:nil, nil];
    [alert show];
}

Code for the exportImages method:

        - (void)exportImages:(NSArray *)imageArray 
           asVideoToPath:(NSString *)path 
           withFrameSize:(CGSize)imageSize
         framesPerSecond:(NSUInteger)fps {
        NSLog(@"Start building video from defined frames.");

        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:imageSize.width], AVVideoWidthKey,
                                       [NSNumber numberWithInt:imageSize.height], AVVideoHeightKey,
                                       nil];

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


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

        NSParameterAssert(videoWriterInput);
        NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
        videoWriterInput.expectsMediaDataInRealTime = YES;
        [videoWriter addInput:videoWriterInput];

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

        CVPixelBufferRef buffer = NULL;

        //convert uiimage to CGImage.
        int frameCount = 0;

        for(UIImage * img in imageArray) {
            buffer = [self pixelBufferFromCGImage:[img CGImage] andSize:imageSize];

            BOOL append_ok = NO;
            int j = 0;
            while (!append_ok && j < 30) {
                if (adaptor.assetWriterInput.readyForMoreMediaData)  {
                    //print out status::
                    NSString *border = @"**************************************************";
                    NSLog(@"\n%@\nProcessing video frame (%d,%d).\n%@",border,frameCount,[imageArray count],border);

                    CMTime frameTime = CMTimeMake(frameCount,(int32_t) fps);
                    append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
                    if(!append_ok){
                        NSError *error = videoWriter.error;
                        if(error!=nil) {
                            NSLog(@"Unresolved error %@,%@.", error, [error userInfo]);
                        }
                    }

                } 
                else {
                    printf("adaptor not ready %d, %d\n", frameCount, j);
                    [NSThread sleepForTimeInterval:0.1];
                }
                j++;
            }
            if (!append_ok) {
                printf("error appending image %d times %d\n, with error.", frameCount, j);
            }
            frameCount++;
        }

        //Finish the session:
        [videoWriterInput markAsFinished];  
        [videoWriter finishWriting];
        NSLog(@"Write Ended");

    }

Paramenters to the method

  • imageArray : NSArray of UIImage.
  • path : Temporary path to write to while you process (temp defined above).
  • imageSize : The size of the video in pixels (width, and height).
  • fps : How many images should be displayed per second in the video.

Hope it helps! Sorry about the formatting - I'm still very new to StackOverflow.com.

This is where I used the code: http://www.youtube.com/watch?v=DDckJyF2bnA

0

精彩评论

暂无评论...
验证码 换一张
取 消