GPUImage使用几点总结

1.使用相机观察滤镜实时效果:

首先定义以下属性

@property (nonatomic, strong) GPUImageStillCamera* Camera;//相机

@property (nonatomic, strong) GPUImageView*GPUImgView;//拍照视图

@property (nonatomic, strong) GPUImageOutput<GPUImageInput>*Filter;//滤镜

在 viewdidlod中这样写

_Camera = [[GPUImageStillCamera alloc]initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];

    _Camera.outputImageOrientation = UIInterfaceOrientationPortrait;

    

    _GPUImgView = [[GPUImageView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.width)];

    _Filter = [[GPUImageHueFilter alloc] init];//你还可以更换其他滤镜如_Filter = [[GPUImageSketchFilter allocinit];

    [_Camera addTarget:_Filter];

    [_Filter addTarget:_GPUImgView];

    [self.view addSubview:_GPUImgView];

    [_Camera startCameraCapture];

当你准备用collectionVIew来实现选择滤镜种类时 在collectionview代理中这样写

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    [_Camera removeAllTargets];

    switch (indexPath.item) {

        case 0: {

            _Filter = [[GPUImageStretchDistortionFilter alloc]init];

            

        }break;

        case 1:

        {

            _Filter = [[GPUImageFilter alloc] initWithFragmentShaderFromFile:@"CustomFilter"];

            

        }break;

        case 2:{

             _Filter = [[GPUImageSepiaFilter alloc] init];

        }

            break;

        case 3:{

            _Filter = [[GPUImageRGBClosingFilter alloc] initWithRadius:4];

        }break;

        case 4:{

            _Filter = [[GPUImageGlassSphereFilter alloc] init];

             [(GPUImageGlassSphereFilter *)_Filter setRadius:0.5];

            GPUImageGaussianBlurFilter *gaussianBlur = [[GPUImageGaussianBlurFilter alloc] init];

            [_Camera addTarget:gaussianBlur];

            gaussianBlur.blurRadiusInPixels = 5.0;

            

            GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];

            blendFilter.mix = 1.0;

            [gaussianBlur addTarget:blendFilter];

            [_Filter addTarget:blendFilter];

            

            [blendFilter addTarget:_mGPUImgView];

        }break;

        case 5:{

             _Filter = [[GPUImageSoftEleganceFilter alloc] init];

        }break;

        case 6:{

            _Filter = [[GPUImageMissEtikateFilter alloc] init];

        }break;

        case 7:{

            _Filter = [[GPUImageAmatorkaFilter alloc] init];

        }break;

        case 8:{

            _Filter = [[GPUImagePinchDistortionFilter alloc] init];

        }break;

        case 9:{

             _Filter = [[GPUImageSwirlFilter alloc] init];

        }break;

        case 10:{

             _Filter = [[GPUImageColorInvertFilter alloc] init];

        }break;

        case 11:{

            _Filter = [[GPUImageSketchFilter alloc] init];

            [(GPUImageSketchFilter *)_Filter setEdgeStrength:0.45];

        }break;

        default:

            break;

    }

    [_Camera addTarget:_Filter];

    [_Filter addTarget:_GPUImgView];


}

如果你要拍照请调用以下方法

-(void)takePhoto{

    

    [_Camera capturePhotoAsJPEGProcessedUpToFilter:_Filter withCompletionHandler:^(NSData *processedJPEG, NSError *error){

     

        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

        

        [library writeImageDataToSavedPhotosAlbum:processedJPEG metadata:_mCamera.currentCaptureMetadata completionBlock:^(NSURL *assetURL, NSError *error2)

         {

             if (error2) {

                 NSLog(@"ERROR: the image failed to be written");

             }

             else {

                 NSLog(@"PHOTO SAVED - assetURL: %@", assetURL);

             }

             

         }];

    }];

}


2.直接用滤镜美化图片:

_Filter = [[GPUImageFilter alloc] initWithFragmentShaderFromFile:@"CustomFilter"];

[_Filter forceProcessingAtSize:_photoImageview.frame.size];//设置渲染尺寸

[_Filter useNextFrameForImageCapture];

    GPUImagePicture* stillImageSource = [[GPUImagePicture alloc]initWithImage:[UIImage imageNamed:@"IMGTEST.jpg"]];

    [stillImageSource addTarget:_Filter];

    [stillImageSource processImage];//渲染

    UIImage*newImage = [_Filter imageFromCurrentFramebuffer];//得到渲染后图片

    _photoImageview.image = newImage;





你可能感兴趣的:(iOS,GPUImage)