您的位置:首页 > 产品设计 > UI/UE

GPUImage使用

2016-01-06 17:44 1066 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: