2012-03-26 49 views
1

我使用AVFoundation框架在我的iPad應用程序來錄製視頻的videos.When錄製完成,我創建使用AVFoundation.But爲同一視頻的縮略圖,如果我記錄沒有太大的兩個視頻那麼縮略圖之間的時間延遲是不能正確創建的。顯然它需要一些延遲。如何避免這種情況,或者至少我怎麼知道前一個視頻的縮略圖是否已經完全創建,這樣我就可以顯示一些「等待」符號那段時間的用戶?AVFoundation框架,需要延遲到創建縮略圖

請幫忙,因爲我無法解決問題。

//delegate method which gets called when recording ends. 
//Here I create the thumb and store it in self.thumb 
-(void)recorder:(AVCamRecorder *)recorder recordingDidFinishToOutputFileURL:(NSURL *)outputFileURL error:(NSError *)error 
{ 
    self.thumb=nil; 
    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:outputFileURL options:nil]; 
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 
    generator.appliesPreferredTrackTransform=TRUE; 
    [asset release]; 
    CMTime thumbTime = CMTimeMakeWithSeconds(0,1); 

    AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){ 
     if (result != AVAssetImageGeneratorSucceeded) { 
      NSLog(@"couldn't generate thumbnail, error:%@", error); 
     } 

     self.thumb=[UIImage imageWithCGImage:im]; 

     [generator release]; 
    }; 

    CGSize maxSize = CGSizeMake(320, 180); 
    generator.maximumSize = maxSize; 
    [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler]; 

    if ([[UIDevice currentDevice] isMultitaskingSupported]) { 
     [[UIApplication sharedApplication] endBackgroundTask:[self backgroundRecordingID]]; 
    }  

    if ([[self delegate] respondsToSelector:@selector(captureManagerRecordingFinished:)]) { 
     [[self delegate] captureManagerRecordingFinished:self]; 
    } 
} 


[self copyFileToDocuments:outputFileURL]; 
} 


//Save video and thumbnail to documents 
- (void) copyFileToDocuments:(NSURL *)fileURL 
{ 
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"]; 
NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/output_%@.mov", [dateFormatter stringFromDate:[NSDate date]]]; 
[dateFormatter release]; 
NSError *error; 
if (![[NSFileManager defaultManager] copyItemAtURL:fileURL toURL:[NSURL fileURLWithPath:destinationPath] error:&error]) { 
    if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) { 
     [[self delegate] captureManager:self didFailWithError:error]; 
    } 
} 
else 
{ 
    destinationPath=[[destinationPath lastPathComponent]stringByDeletingPathExtension]; 
    [self saveImage:self.thumb withName:destinationPath]; 
} 

} 

    //Method Where I save self.thumb in app documents 
-(void) saveImage:(UIImage*)image withName:(NSString*)fileName 
{ 
NSFileManager *fileManager = [NSFileManager defaultManager]; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 
    //Save Image 
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format. 

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",fileName]]; 

[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; 

} 

以上是我的代碼。 但如果我不給兩個錄音延遲,self.thumb沒有得到正確保存。

+0

你可以請張貼你的代碼嗎? – 2012-03-26 13:42:35

+0

我用代碼更新了我的問題。 – Yogi 2012-03-26 13:55:15

+0

我發現零時間需要很長時間才能生成縮略圖。嘗試使用'CMTimeMakeWithSeconds(1,1)'的時間,除非剪輯更短。 – jessecurry 2013-10-03 21:53:27

回答

0

我使用了不同的方法。當用戶點擊'錄製'時,我首先點擊相機中的一張照片,然後開始錄製視頻。後來我使用與縮略圖相同的照片。

我知道這是不是最好的方法,但最好的事情是它的工作;)

希望這可以幫助別人同樣的問題。