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没有得到正确保存。
你可以请张贴你的代码吗? – 2012-03-26 13:42:35
我用代码更新了我的问题。 – Yogi 2012-03-26 13:55:15
我发现零时间需要很长时间才能生成缩略图。尝试使用'CMTimeMakeWithSeconds(1,1)'的时间,除非剪辑更短。 – jessecurry 2013-10-03 21:53:27