Mikael
Mikael

Reputation: 3612

GPUImage - MovieWriter, nothing happens for a while after "setCompletionBlock"

Been struggling with this for a couple of days now. I'm processing a video with a filter, it saves the video just fine. However, after it's saved, it takes a long time to update the UI. I can see the video in iTunes (with iTunes file sharing), a long time before the UI is updated.

I create the view like this, and add that to my view controller. This is just so the user can preview the video and select filter.

-(GPUImageView*)playClipWithClip:(MYClip*)clip
{
    _clip = clip;
    _filterView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 0, 568, 320)];

    _movieFile = [[GPUImageMovie alloc] initWithURL:[self urlForCurrentClip]];
    _movieFile.runBenchmark = NO;
    _movieFile.playAtActualSpeed = YES;
    _movieFile.shouldRepeat = YES;

    [self changeFilter];
    return _filterView;
}

When the user wants to save the video I have this method:

-(void)saveClipWithFilter
{
    [_movieFile cancelProcessing];

    _movieFile.runBenchmark = YES;
    _movieFile.playAtActualSpeed = NO;
    _movieFile.shouldRepeat = NO;

    NSString *movieName = [self fileNameForGeneratedClip];
    NSString *generatedMovieNameWithPath = [NSString stringWithFormat:@"Documents/%@",movieName];

    NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:generatedMovieNameWithPath];
    unlink([pathToMovie UTF8String]);
    NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

    _movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(568, 320.0)];
    [_filter addTarget:_movieWriter];

    _movieWriter.shouldPassthroughAudio = NO;
    [_movieFile enableSynchronizedEncodingUsingMovieWriter:_movieWriter];

    [_movieWriter startRecording];
    [_movieFile startProcessing];

    __weak typeof(self) weakSelf = self;
    [_movieWriter setCompletionBlock:^{
        NSLog(@"**************************** DONE ****************************");
        [weakSelf.filter removeTarget:weakSelf.movieWriter];
        [weakSelf.movieWriter finishRecording];

        [weakSelf exitVideoEditingModeAndSave];
    }];
}

My method [weakSelf exitVideoEditingModeAndSave]; is called. And that method in turn calls the delegate (my view controller).

The problem is that after my delegate is called and my NSLog shows, it will take about 10 seconds for the view to update. I know that the file is ready and has been saved.

Any ideas?

Upvotes: 1

Views: 738

Answers (1)

Jack
Jack

Reputation: 16855

This is a threading issue, in your completion block, dispatch to the main thread before you update any UI elements

Upvotes: 3

Related Questions