Ios GPUImage视频动画

Ios GPUImage视频动画,ios,gpuimage,Ios,Gpuimage,我用GPUImage在我录制的视频上显示一些覆盖图。现在我用GPUImageAlphaBlendFilter在视频中显示图像,但现在我需要用一些“弹出”动画来显示这个图像。我想在这张图片上做一个从很小到正常大小的缩放,但我的问题是我真的不知道怎么做。有没有办法在录制视频时制作动画?下面是我目前拥有的代码: -(void)setUpCameraWithPosition:(bool)switchToFrontCamera { if(videoCamera != nil) { [videoCa

我用GPUImage在我录制的视频上显示一些覆盖图。现在我用GPUImageAlphaBlendFilter在视频中显示图像,但现在我需要用一些“弹出”动画来显示这个图像。我想在这张图片上做一个从很小到正常大小的缩放,但我的问题是我真的不知道怎么做。有没有办法在录制视频时制作动画?下面是我目前拥有的代码:

-(void)setUpCameraWithPosition:(bool)switchToFrontCamera
{
if(videoCamera != nil)
{
    [videoCamera stopCameraCapture];
}

if(switchToFrontCamera) {
    videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionFront];
}
else {
    videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
}

videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
videoCamera.horizontallyMirrorFrontFacingCamera = NO;
videoCamera.horizontallyMirrorRearFacingCamera = NO;

filter = [GPUImageBrightnessFilter new];
filter.brightness = 0.0;

GPUImageTransformFilter *transitionFilter = [[GPUImageTransformFilter alloc] init];
CGAffineTransform trans = CGAffineTransformTranslate(CGAffineTransformIdentity, 0.0, 0.5);
[transitionFilter setAffineTransform:trans];

GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
blendFilter.mix = 1.0;

UIImage *img = [UIImage imageNamed:@"logo-background-medium-cropped.png"];
iv = [[UIImageView alloc] initWithImage:img];
iv.backgroundColor = [UIColor clearColor];

[videoCamera addTarget:filter];
GPUImageView *filterView = (GPUImageView *)self.preview;
filterView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;

NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
NSLog(@"%@",pathToMovie);
unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0)];
movieWriter.encodingLiveVideo = YES;
movieWriter.shouldPassthroughAudio = YES;

uiElementInput = [[GPUImageUIElement alloc] initWithView:iv];
[filter addTarget:blendFilter atTextureLocation:0];
[uiElementInput addTarget:transitionFilter];
[transitionFilter addTarget:blendFilter atTextureLocation:1];

[blendFilter addTarget:filterView];
[blendFilter addTarget:movieWriter];

[videoCamera startCameraCapture];

__unsafe_unretained GPUImageUIElement *weakUIElementInput = uiElementInput;
[filter setFrameProcessingCompletionBlock:^(GPUImageOutput * filter, CMTime frameTime){
    [weakUIElementInput update];
}];
}

-(IBAction)stopCameraTouchUp:(id)sender
{
if(recording)
{
    saving = YES;
    recording = NO;
    dispatch_after(0, dispatch_get_main_queue(), ^(void) {
        [filter removeTarget:movieWriter];
        videoCamera.audioEncodingTarget = nil;
        [movieWriter finishRecording];
        NSLog(@"Movie completed");

        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
        NSLog(@"%@",path);
        ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
        [al writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error) {
            if (error) {
                NSLog(@"Error %@", error);
            } else {
                NSLog(@"Success");
                recordButton.hidden = NO;
                [videoCamera stopCameraCapture];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    });
}
else if(!saving)
{
    recording = NO;
    [self.navigationController popViewControllerAnimated:YES];
}
}

- (IBAction)startRecordingTouchUp:(id)sender
{
__unsafe_unretained GPUImageUIElement *weakUIElementInput = uiElementInput;

[filter setFrameProcessingCompletionBlock:^(GPUImageOutput * filter, CMTime frameTime){

    [weakUIElementInput update];
}];

dispatch_after(0.0, dispatch_get_main_queue(), ^(void) {
    NSLog(@"Start recording");
    recording = YES;
    [movieWriter startRecording];

});
}

如果我正确理解了你的问题,你想把动画图像写入视频 我在GPUImage拉取请求中找到了可能的解决方案

更改了GPUImagePicture.m并添加了在特定帧时间写入图像的功能。 他还添加了一个如何使用图像制作动画的示例

  + (void)zoomInAndWriteToMovie:(UIImage*)image
{
    NSString* moviePath = @"output.mp4";
    NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject];
    NSURL* movieURL = [NSURL URLWithString:moviePath relativeToURL: [NSURL fileURLWithPath:documentPath isDirectory:YES]];
    GPUImageMovieWriter *movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(1280,720)];

    GPUImagePicture *imageSource = [[GPUImagePicture alloc] initWithImage:image];
    GPUImageTransformFilter *transformFilter = [[GPUImageTransformFilter alloc] init];
    [imageSource addTarget:transformFilter];

    [transformFilter addTarget:movieWriter];
    [movieWriter startRecording];

    const int totalFrames = 100;
    const float startZoomRatio = 1.2f;
    const float endZoomRatio = 1.0f;
    for (int i = 0; i < totalFrames; i++) {
        float currentZoomRatio = startZoomRatio + (endZoomRatio - startZoomRatio) * i / (float)totalFrames;
        CGAffineTransform scaleTransform = CGAffineTransformMakeScale(currentZoomRatio, currentZoomRatio);

        transformFilter.affineTransform = scaleTransform;
        imageSource.imageFrameTime = CMTimeMake(i, 30); // 30 FPS

        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        [imageSource processImageWithCompletionHandler:^{
            dispatch_semaphore_signal(semaphore);
        }];

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    }
                [[GPUImageContext sharedFramebufferCache] purgeAllUnassignedFramebuffers];
    [transformFilter removeTarget:movieWriter];

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    [movieWriter finishRecordingWithCompletionHandler:^{
        dispatch_semaphore_signal(semaphore); // Force to wait for the completion, it might be causing bugs to continue without waiting.
    }];

    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    [transformFilter removeAllTargets];
    [imageSource removeAllTargets];
}
+(void)zoomInAndWriteToMovie:(UIImage*)图像
{
NSString*moviePath=@“output.mp4”;
NSString*documentPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
NSURL*movieURL=[NSURL URLWithString:moviePath relativeToURL:[NSURL fileURLWithPath:documentPath isDirectory:YES]];
GPUImageMovieWriter*movieWriter=[[GPUImageMovieWriter alloc]initWithMovieURL:movieURL大小:CGSizeMake(1280720)];
GPUImagePicture*imageSource=[[GPUImagePicture alloc]initWithImage:image];
GPUImageTransformFilter*transformFilter=[[GPUImageTransformFilter alloc]init];
[imageSource addTarget:transformFilter];
[目标:电影编剧];
[电影编剧startRecording];
const int totalFrames=100;
常数浮点数startZoomRatio=1.2f;
常量浮点endZoomRatio=1.0f;
对于(int i=0;i