2012-03-26 47 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

我使用了不同的方法。当用户点击'录制'时,我首先点击相机中的一张照片,然后开始录制视频。后来我使用与缩略图相同的照片。

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

希望这可以帮助别人同样的问题。