简体   繁体   English

Iphone SDK,从UIImage创建视频

[英]Iphone SDK,Create a Video from UIImage

i needs to create a video from the image selected. 我需要从所选图像中创建视频。

i have code it shoudl work but its giving error while appending buffer. 我有代码它shoudl工作,但它在附加缓冲区时给出错误。

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



}

on the other view controller i am showing this image on a uiimage view. 在另一个视图控制器上,我在uiimage视图上显示此图像。

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???? 我被卡住了,任何人都可以帮我这个???? :( :(

i have figured out the problem. 我已经找到了问题所在。

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 我还没有得到它的工作原因,但得到了解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM