4

I want to add different effects to an UIImage. Currently I am trying with using GUIImage library. https://github.com/BradLarson/GPUImage But, I am unable to run most of the examples provided. Only working example for me is FilterShowcase It works great.

But the problem is, its work only with camera. What i want is Load an static image on to a UIImageView and apply those filters.

I tried, but unable to do it.

This is how I tried it

  - (void)setupFilter;
{
//    videoCamera = [[GPUImageVideoCamera alloc]  initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
////    videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionFront];
//    videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;


UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 350)];

imgView.Image=[UIImage imageNamed:@"WID-small.jpg"];

[self.view addSubview:imgView];

BOOL needsSecondImage = NO;

switch (filterType)
{
    case GPUIMAGE_SEPIA:
    {
        self.title = @"Sepia Tone";
        self.filterSettingsSlider.hidden = NO;

        [self.filterSettingsSlider setValue:1.0];
        [self.filterSettingsSlider setMinimumValue:0.0];
        [self.filterSettingsSlider setMaximumValue:1.0];

        filter = [[GPUImageSepiaFilter alloc] init];

        sourcePicture = [[GPUImagePicture alloc] initWithImage:imgView.image smoothlyScaleOutput:YES];
        [sourcePicture processImage];            
        [sourcePicture addTarget:filter];




    }; break;

Actually what I tried to do is, Load a still image instead of videoCamera. But it doesn't work. If anyone can help me, its highly appreciated.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
smartsanja
  • 4,413
  • 9
  • 58
  • 106
  • try to use Core Image Filter for static images simple and easy option... – Leena Jul 16 '12 at 06:27
  • How did you get this working with your slider? My slider changes values on my sepia filter but nothing changes in my GPUImageView... – jjxtra Oct 24 '12 at 07:16

1 Answers1

12

First, there are a couple of reasons why the other sample applications might not be building for you. As is stated in this answer, make sure that you have the scheme for the application selected in the upper left of your Xcode window, not the GPUImage framework project. If changing that doesn't help, exit out of Xcode, delete the relevant project directories from your DerivedData directory, and restart Xcode. That sometimes seems to be needed due to a bug in recent Xcode versions.

The filtering of an image is described in the documentation for the project, which is in the Readme.md file and on the page you link above. In particular, see the section titled "Processing a still image":

There are a couple of ways to process a still image and create a result. The first way you can do this is by creating a still image source object and manually creating a filter chain:

UIImage *inputImage = [UIImage imageNamed:@"Lambeau.jpg"];

GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageSepiaFilter *stillImageFilter = [[GPUImageSepiaFilter alloc] init];

[stillImageSource addTarget:stillImageFilter];
[stillImageSource processImage];

UIImage *currentFilteredVideoFrame = [stillImageFilter imageFromCurrentlyProcessedOutput];

For single filters that you wish to apply to an image, you can simply do the following:

GPUImageSepiaFilter *stillImageFilter2 = [[GPUImageSepiaFilter alloc] init];
UIImage *quickFilteredImage = [stillImageFilter2 imageByFilteringImage:inputImage];

The SimpleImageFilter example application shows how to do this in more detail.

Community
  • 1
  • 1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • Thanks a lot for the detailed reply. And I'm very very happy to get an answer from person like you sir. Now its work fine. I applied filters and then show the image on a imageView. But only problem I have now is Value changes are not function (Slider value changes not apply for the image). – smartsanja Jul 17 '12 at 04:59
  • Brad, how I can mix two filters to apply in the same image? I can't find =( Currently, I am using one filter, get the result, and apply the 2nd filter to get the final result. Thanks for the awesome framework! – Natan R. Jul 23 '12 at 08:30
  • @NatanR. - I'm not sure what you're asking. You can simply set up a filter chain (add one filter as the target of the next filter) to have one filter be applied, then the other. For mixing two images, you can use blend filters. – Brad Larson Jul 23 '12 at 14:13