iPhone SDK,从UIImage创建视频

4

我需要从所选图像创建视频。

我编写了代码,但在附加缓冲区时出现错误。

这是两种类型的图像保存方式。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
{
    //    NSLog(@"Came From Here");
    imgv.image = img;   
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    app.imgmain = img;
    [self dismissModalViewControllerAnimated:YES];
    RecordVoice *rec = [[RecordVoice alloc] initWithNibName:@"RecordVoice" bundle:nil];
    rec.hidesBottomBarWhenPushed = YES;
    // rec.img.image = img;
    [self.navigationController pushViewController:rec animated:YES];
    //[self presentModalViewController:rec animated:YES];
    [rec release];
    //  flag =@"yes";
    // need to show the upload image button now
    //  [username, ititle resignFirstResponder];



}

在另一个视图控制器中,我在一个UIImageView上显示这张图片。当点击按钮时,我使用以下代码将该图片转换为视频。
-(void)createVideoFile
{

        NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectoryPath error:nil];
        for (NSString *tString in dirContents) {
            if ([tString isEqualToString:@"test.mp4"]) 
            {
                [[NSFileManager defaultManager]removeItemAtPath:[NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,tString] error:nil];

            }
        }
    NSString *nfile = [documentsDirectoryPath stringByAppendingPathComponent:@"test.mp4"];
    AVURLAsset * urlAsset = [AVURLAsset URLAssetWithURL:recordedTmpFile options:nil];

        NSLog(@"Write Started");

        NSError *error = nil;
    CGSize size = img.image.size; //CGSizeMake(320, 480);




    NSLog(@"Write Started");


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

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

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


    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;

        buffer = [self pixelBufferFromCGImage:[img.image CGImage] andSize:size];

        BOOL append_ok = NO;
        int j = 0;
        while (!append_ok && j < 30) 
        {
            if (adaptor.assetWriterInput.readyForMoreMediaData) 
            {
                printf("appending %d attemp %d\n", frameCount, j);

                CMTime frameTime = urlAsset.duration;//CMTimeMake(frameCount,(int32_t) 10);
                append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];

                //if(buffer)
                  //  CVBufferRelease(buffer);
                [NSThread sleepForTimeInterval:0.05];
            } 
            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", frameCount, j);
        }
        frameCount++;


        [videoWriterInput markAsFinished];  
         [videoWriter finishWriting];




    [self CompileFilesToMakeMovie];
    [altv dismissWithClickedButtonIndex:0 animated:YES];
    [altv release];



        NSLog(@"Write Ended");


}

但是这并不起作用...

我被卡住了,有人可以帮帮我吗? :(


嗨,我能制作视频,但是视频不是从原始图像中生成的...它是一种锯齿状的视频... - Rajneesh071
不,问题出在 .png 图片上... 当我使用 jpg 图片时一切都正常。 - Rajneesh071
@Rajneesh071 不太确定...我还没有尝试过使用.png类型的图像...我正在尝试使用从iPhone相机本身获取的图像。所以不确定。但我不认为.png会在这里创建问题... - Darshan
@Darshan能否清楚地告诉我,如何从单个图像中获取视频文件,使其作为整个视频的常量图像,并混合音频文件。视频的长度应该等于音频文件的长度。 - Himanshu
@Darshan我使用了相同的代码并创建了视频,它可以正常工作,但是对于20多张图像应用程序会在设备上崩溃,我查看了模拟器并发现对于更多的图像,它需要1GB +内存,所以这个代码有什么问题吗?你能告诉我可能出了什么问题吗...谢谢 - Satish
显示剩余15条评论
1个回答

0
我找到问题所在。
如果我们使用大尺寸的图片,它将无法正常工作。例如从相机应用程序拍摄的照片具有较大的尺寸。
因此,我将其压缩到低级别,然后它就可以工作了。
我还不知道为什么它能够工作,但是找到了解决方案。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接