当我的应用程序进入后台时,我遇到了将视频从GPUImage videoCamera保存到相机胶卷的问题。当应用程序返回到前台/重新启动时,该文件仅保存到相机胶卷。我毫无疑问会犯一个初学者代码错误,如果有人可以指出它将不胜感激。
- (void)applicationDidEnterBackground:(UIApplication *)application {
if (isRecording){
[self stopRecording];
};
if (self.isViewLoaded && self.view.window){
[videoCamera stopCameraCapture];
};
runSynchronouslyOnVideoProcessingQueue(^{
glFinish();
});
NSLog(@"applicationDidEnterBackground");
然后
-(void)stopRecording {
[filterBlend removeTarget:movieWriter];
videoCamera.audioEncodingTarget = nil;
[movieWriter finishRecording];
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"file.mov"];
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
[al writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"Error %@", error);
} else {
NSLog(@"Success");
}
}];
isRecording = NO;
NSLog(@"Stop recording");
答案 0 :(得分:1)
正如布拉德在他的指出中那样,正如通常那样富有洞察力的评论,-writeVideoAtPathToSavedPhotosAlbum:completionBlock:直到应用程序返回到前台后才完成,我通过添加
来解决它self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"Background handler called. Not running background tasks anymore.");
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
}];
和
@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask;
找到此解决方案