GPUImage使用

GIit: https://github.com/BradLarson/GPUImage/downloads

1.打开Framework中GPUImage.xcodeproj,运行项目
2.在targets->build Rules中拷贝头文件
3.运行项目后将products中.a文件和头文件放一起加入工程

如下
这里写图片描述

图像处理

        UIImage* imageOri =[self processUsingGPUImage: [UIImage imageNamed:@"960.png"]];

    UIImageView *imageOriView = [[UIImageView alloc]initWithFrame:self.view.frame];
    imageOriView.image = imageOri;
    [self.view addSubview:imageOriView];
    //曝光
    GPUImageExposureFilter *passthroughFilter2 = [[GPUImageExposureFilter alloc]init];
    passthroughFilter2.exposure = 2;
    [passthroughFilter2 forceProcessingAtSize:imageOri.size];
    GPUImagePicture *stillImageSource2 = [[GPUImagePicture alloc]initWithImage:imageOri];
    [stillImageSource2 addTarget:passthroughFilter2];
    [stillImageSource2 processImage];
    UIImage *nearestNeighborImage2 = [UIImage imageWithCGImage:[passthroughFilter2 newCGImageFromCurrentlyProcessedOutput]]
    ;
    UIImageView *imageView2 = [[UIImageView alloc]initWithImage:nearestNeighborImage2];
    imageView2.frame = CGRectMake(5, 120, 150,  90);
    [self.view addSubview:imageView2];
    GPUImageHueFilter *passthroughFilter3 = [[GPUImageHueFilter alloc]init];
    passthroughFilter3.hue = 20;
    [passthroughFilter3 forceProcessingAtSize:imageOri.size];
    GPUImagePicture *stillImageSource3 = [[GPUImagePicture alloc]initWithImage:imageOri];
    [stillImageSource3 addTarget:passthroughFilter3];
    [stillImageSource3 processImage];
    UIImage *nearestNeighborImage3 = [UIImage imageWithCGImage:[passthroughFilter3 newCGImageFromCurrentlyProcessedOutput]];
    UIImageView *imageView3 = [[UIImageView alloc]initWithImage:nearestNeighborImage3];
    imageView3.frame = CGRectMake(0, 0, 150,  200);
    [self.view addSubview:imageView3];

- (UIImage *)processUsingGPUImage:(UIImage*)input {
    // 1. Create the GPUImagePictures
    GPUImagePicture * inputGPUImage = [[GPUImagePicture alloc] initWithImage:input];
    UIImage * ghostImage = [self createPaddedGhostImageWithSize:input.size];
    GPUImagePicture * ghostGPUImage = [[GPUImagePicture alloc] initWithImage:ghostImage];
    // 2. Set up the filter chain
    GPUImageAlphaBlendFilter * alphaBlendFilter = [[GPUImageAlphaBlendFilter alloc] init];
    alphaBlendFilter.mix = 0.5;
    [inputGPUImage addTarget:alphaBlendFilter atTextureLocation:0];
    [ghostGPUImage addTarget:alphaBlendFilter atTextureLocation:1];
    GPUImageGrayscaleFilter * grayscaleFilter = [[GPUImageGrayscaleFilter alloc] init];
    [alphaBlendFilter addTarget:grayscaleFilter];
    // 3. Process & grab output image
    [grayscaleFilter useNextFrameForImageCapture];
    [inputGPUImage processImage];
    [ghostGPUImage processImage];
    UIImage * output = [grayscaleFilter imageFromCurrentFramebuffer];
    return output;
}
- (UIImage *)createPaddedGhostImageWithSize:(CGSize)inputSize {
    UIImage * ghostImage = [UIImage imageNamed:@"960.png"];
    CGFloat ghostImageAspectRatio = ghostImage.size.width / ghostImage.size.height;
    NSInteger targetGhostWidth = inputSize.width * 0.25;
    CGSize ghostSize = CGSizeMake(targetGhostWidth, targetGhostWidth / ghostImageAspectRatio);
    CGPoint ghostOrigin = CGPointMake(inputSize.width * 0.5, inputSize.height * 0.2);
    CGRect ghostRect = {ghostOrigin, ghostSize};
    UIGraphicsBeginImageContext(inputSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect inputRect = {CGPointZero, inputSize};
    CGContextClearRect(context, inputRect);
    CGAffineTransform flip = CGAffineTransformMakeScale(1.0, -1.0);
    CGAffineTransform flipThenShift = CGAffineTransformTranslate(flip,0,-inputSize.height);
    CGContextConcatCTM(context, flipThenShift);
    CGRect transformedGhostRect = CGRectApplyAffineTransform(ghostRect, flipThenShift);
    CGContextDrawImage(context, transformedGhostRect, [ghostImage CGImage]);
    UIImage * paddedGhost = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return paddedGhost;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值