2011-10-24 52 views
4

我需要從所選圖像創建一個視頻。Iphone SDK,從UIImage創建視頻

我有代碼它shoudl工作,但它給錯誤,而附加緩衝區。

這就是這兩種圖像的保存方式。

-(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 [email protected]"yes"; 
    // need to show the upload image button now 
    // [username, ititle resignFirstResponder]; 



} 

對另一個視圖控制器我在uiimage視圖上顯示此圖像。

並按鈕單擊我將該圖像轉換爲視頻與此代碼。

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


} 

但是,這是不工作...

我堅持任何人都可以請幫我在這???? :(

+0

@MuTaTeD惠當我使用的代碼的JPEG則它不會給錯誤,但它不會創建一個視頻。但是當我使用H264Codec它給錯誤。我也嘗試過你的代碼,但它給了錯誤河這幾乎只是你的代碼。 – Darshan

+0

你有沒有設置混音(非獨佔)音頻會話類別? –

+0

@有節奏的是向它添加一個音頻文件。但後來病房後視頻創建...但我已經找到了問題...我已經回答了它。 – Darshan

回答

0

我想通了這個問題。

如果我們使用它不會工作。就像從相機應用拍攝的照片有一個大尺寸。

所以我壓縮到大尺寸圖片低級別,然後它的工作原理..

我沒有還沒有完全進入爲什麼它的工作等,但是得到了解決