0

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

1 回答 1

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找到了这个解决方案

于 2013-10-31T21:16:05.843 回答