Iphone SDK,从UIImage创建视频 [英] Iphone SDK,Create a Video from UIImage

查看:190
本文介绍了Iphone SDK,从UIImage创建视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据所选图像创建一个视频。

i needs to create a video from the image selected.

我有代码可以正常工作,但是在附加缓冲区时给出错误。

i have code it shoudl work but its giving error while appending buffer.

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

This is how both type of images has been saved.

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



}

在另一个视图上控制器我在uiimage视图上显示此图像。

on the other view controller i am showing this image on a uiimage view.

并点击按钮我正在使用此代码将该图像转换为视频。

and on button click i am converting that image to video with this code.

-(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");


}

但这不起作用......

But this is not working...

我被卡住了,任何人都可以帮助我吗???? :(

i am stuck can anyone please help me in this???? :(

推荐答案

我已经找到了问题。

如果我们使用大尺寸的图像它不会工作。就像从相机应用程序拍摄的照片有一个大尺寸。

if we use image with big size it wont work. like the pictures taken from camera app has a big size.

所以我将它压缩到低水平,然后它的工作原理。 。

so i am compressing it to low level and then it works..

我还没知道为什么它的工作方式却得到了解决方案

i didnt yet got why its working like but got the solution

这篇关于Iphone SDK,从UIImage创建视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆