天天看点

iOS GPUImage研究七:动态相册初探(水印)

前言:

其实,不仅仅是水印,包含一些3D立体相册的灵感。可以通过GPUImage来实现
试想一下,我们可以通过手机录制视频,然后能够实现自己添加特效,制作成动感影集,是不是很激动。
           

实现效果:

iOS GPUImage研究七:动态相册初探(水印)

说明:

实现了一个简单地动画,逻辑是,A视图跟随B视图转动,但是A视图的尺寸仅仅被B视图包裹在内,随之变动。
其实很简单:
           
imageView1.frame = CGRectMake(imageView2.frame.origin.x+, imageView2.frame.origin.y+, imageView2.frame.size.width+, imageView2.frame.size.height+);

imageView2.layer.transform = CATransform3DRotate(imageView2.layer.transform, M_PI/, ,  , );
           
一个简单地CATransform3D动画。
           

关于视频水印,请看上一篇博客

http://blog.csdn.net/xoxo_x/article/details/71055867

介绍一下:GPUImageUIElement

其创建方式和对象方法:

// Initialization and teardown
- (id)initWithView:(UIView *)inputView;
- (id)initWithLayer:(CALayer *)inputLayer;

// Layer management
- (CGSize)layerSizeInPixels;
- (void)update;
- (void)updateUsingCurrentTime;
- (void)updateWithTimestamp:(CMTime)frameTime;

           

注意:

我们需要获取水印的动态,所以需要得到视频的时间戳,因此需要用到updateWithTimestamp这个函数。
如果我们不用的话,我们可以通过一个定时器来更新,但是获取当前时间的时间戳是麻烦的
           

监控视频时间:

这也是一个处理视频进度的一个回调。
           
GPUImageFilter* progressFilter = [[GPUImageFilter alloc] init];
[videoCamera addTarget:progressFilter];
[progressFilter addTarget:filter];

//达到获取当前处理时间的时间戳的目的
[progressFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *output, CMTime time) {

imageView1.frame = CGRectMake(imageView2.frame.origin.x+, imageView2.frame.origin.y+, imageView2.frame.size.width+, imageView2.frame.size.height+);

imageView2.layer.transform = CATransform3DRotate(imageView2.layer.transform, M_PI/, ,  , );

        [strongSelf->pictureView updateWithTimestamp:time];
    }];
           

全部代码:

//
//  ViewController.m
//  WatermarkDemo
//
//  Created by 冯士魁 on //
//  Copyright © 年 xoxo_x. All rights reserved.
//
/**


 在这里你会发现更多,好玩的事情
 *  http://blog.csdn.net/xoxo_x/article
 *
 *
 */

#import "ViewController.h"
#import "GPUImage.h"

@interface ViewController (){
//    GPUImagePicture *pictureFile;
    GPUImageOutput<GPUImageInput> *filter;
    GPUImageVideoCamera *videoCamera;
    GPUImageView *filterView;
    GPUImageUIElement * pictureView;
    GPUImageMovie * movieFile;

}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initGPUImageView];
    [self initFilter];
    [self initCamera];



    UIView *view = [[UIView alloc]initWithFrame:self.view.bounds];
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(, , self.view.bounds.size.width/, self.view.bounds.size.height/)];
    imageView1.image = [UIImage imageNamed:@"美女1.jpg"];
    [view addSubview:imageView1];



    UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/, self.view.bounds.size.height/, self.view.bounds.size.width/, self.view.bounds.size.height/)];
    imageView2.image = [UIImage imageNamed:@"美女2.jpg"];
    [view addSubview:imageView2];



    pictureView = [[GPUImageUIElement alloc]initWithView:view];



    GPUImageFilter* progressFilter = [[GPUImageFilter alloc] init];
    [videoCamera addTarget:progressFilter];


    [progressFilter addTarget:filter];
    [pictureView addTarget:filter];

    [filter addTarget:filterView];


    [videoCamera startCameraCapture];

    __strong typeof(self) strongSelf = self;

    [progressFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *output, CMTime time) {

        imageView1.frame = CGRectMake(imageView2.frame.origin.x+, imageView2.frame.origin.y+, imageView2.frame.size.width+, imageView2.frame.size.height+);

        imageView2.layer.transform = CATransform3DRotate(imageView2.layer.transform, M_PI/, ,  , );

        [strongSelf->pictureView updateWithTimestamp:time];
    }];
}


-(void)initGPUImageView{
    filterView = [[GPUImageView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:filterView];
}


-(void)initFilter{
    filter = [[GPUImageAlphaBlendFilter alloc] init];

}

-(void)initCamera{
    videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
    videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    videoCamera.horizontallyMirrorFrontFacingCamera = YES;
}
@end
           

欢迎打赏 – 打赏后、加好友哦 O(∩_∩)O哈哈~

iOS GPUImage研究七:动态相册初探(水印)