GPUImage在后台问题中保存视频

时间:2013-10-31 06:46:34

标签: gpuimage

当我的应用程序进入后台时,我遇到了将视频从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");

1 个答案:

答案 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;

http://www.raywenderlich.com/29948/backgrounding-for-ios

找到此解决方案