0
votes

Searched around on SO for an answer. Found some interesting stuff but I am still stuck.

In a view controller I have a UIImageView which I set an image taken from UIImagePicker when the view loads. (I also set a CIImage at the same time)

I have two sliders (one for brightness, one for contrast). Upon moving the slider, a filter is applied to the CIImage, then the CIImage is rendered into the the UIImageView's UIImage.

Fist off, the UIImage taken from UIImagePicker does show up correctly when first selected.

I also have my slider ranges set correctly and have verified the proper float values are being passed to the delegate functions (via NSLog).

However, when I try playing with the sliders, my UIImageView turns white! Perhaps you folks can help. I'll post some code snippets:

First, my @interface:

@interface PrepViewController : UIViewController <UIImagePickerControllerDelegate , UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *editingImage;
@property (weak, nonatomic) CIImage *editingCIImage;
@property (weak, nonatomic) CIContext *editingCIContext;
@property (weak, nonatomic) CIFilter *editingCIFilter;
@property (nonatomic) BOOL didAskForImage;
@property (weak, nonatomic) IBOutlet UISlider *brightnessSlider;
@property (weak, nonatomic) IBOutlet UISlider *contrastSlider;


- (void) doImageFilter:(NSString *)filter withValue:(float)value;

@end

My viewDidLoad, pretty simple just sets didAskForImage to NO

- (void)viewDidLoad
{

    [super viewDidLoad];

    self.didAskForImage = NO;

}

viewDidAppear is where I check if I've already asked for the image, then ask for it

- (void)viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];

if(!self.didAskForImage){

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    picker.delegate = self;
    picker.allowsEditing = NO;

    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypeCamera;
    }

    picker.sourceType = sourceType;

    [self presentModalViewController:picker animated:YES];

}


}

Here is the imagePickerController didFinishPickingImage delegate method this is where I actually set the UIImageView's image property and initialize the CIImage, CIContext, and CIFilter objects.

I also set the didAskForImage boolean and the slider targets.

-(void)imagePickerController:(UIImagePickerController *)picker
  didFinishPickingImage : (UIImage *)image
             editingInfo:(NSDictionary *)editingInfo
{

self.didAskForImage = YES;

self.editingImage.image = image;

self.editingCIImage = [[CIImage alloc] initWithImage:image];
self.editingCIContext = [CIContext contextWithOptions:nil];


self.editingCIContext = [CIContext contextWithOptions:nil];
self.editingCIFilter = [CIFilter filterWithName:@"CIColorControls" keysAndValues:kCIInputImageKey, self.editingCIImage, nil];

[self.contrastSlider addTarget:self action:@selector(contrastMove:) forControlEvents:UIControlEventValueChanged];

[self.brightnessSlider addTarget:self action:@selector(brightnessMove:) forControlEvents:UIControlEventValueChanged];

[picker dismissModalViewControllerAnimated:YES];
}

and the cancel delegate

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)  picker
{
[picker dismissModalViewControllerAnimated:YES];
picker = nil;
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];

}

Here are the sliders' delegate methods. Again, the NSLogs display the expected values.

- (void) contrastMove:(id)sender{

NSLog(@"%f", [(UISlider *)sender value]);

[self doImageFilter:@"inputContrast" withValue:[(UISlider *)sender value]];

}

- (void) brightnessMove:(id)sender{

NSLog(@"%f", [(UISlider *)sender value]);

[self doImageFilter:@"inputBrightness" withValue:[(UISlider *)sender value]];

}

And finally, here is the doImageFilter method I created, which actually does the filter and reassigns the UIImageView (this is where it turns white)

- (void) doImageFilter:(NSString *)filter withValue:(float)value{

[self.editingCIFilter setValue:[NSNumber numberWithFloat:value] forKey:filter];

CIImage *output = [self.editingCIFilter outputImage];
CGImageRef cgi = [self.editingCIContext createCGImage:output fromRect:[output extent]];

self.editingCIImage = output;
self.editingImage.image = [UIImage imageWithCGImage:cgi];

}

So I have a hunch that the CIImage isn't even being initialized properly in the first place; but I don't know why. Been at this for a couple hours now.

Thanks for all your help!

Cheers

1

1 Answers

1
votes

Hahaha, oh wow.

So I figured it out, my CIContext and CIFilter need to be set to strong instead of weak.

Awesome.