title: Core Image help: Setting CIImage from UIImage via UIImagePicker (then filtering it)
body: <p>Searched around on SO for an answer. Found some interesting stuff but I am still stuck.</p>
  
  <p>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)</p>
  
  <p>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.</p>
  
  <p>Fist off, the UIImage taken from UIImagePicker does show up correctly when first selected.</p>
  
  <p>I also have my slider ranges set correctly and have verified the proper float values are being passed to the delegate functions (via NSLog).</p>
  
  <p>However, when I try playing with the sliders, my UIImageView turns white! Perhaps you folks can help. I'll post some code snippets:</p>
  
  <p>First, my @interface:</p>
  
  <pre><code>@interface PrepViewController : UIViewController &lt;UIImagePickerControllerDelegate , UINavigationControllerDelegate&gt;
  
  @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
  </code></pre>
  
  <p>My viewDidLoad, pretty simple just sets didAskForImage to NO</p>
  
  <pre><code>- (void)viewDidLoad
  {
  
      [super viewDidLoad];
  
      self.didAskForImage = NO;
  
  }
  </code></pre>
  
  <p>viewDidAppear is where I check if I've already asked for the image, then ask for it</p>
  
  <pre><code>- (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];
  
  }
  
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>I also set the didAskForImage boolean and the slider targets.</p>
  
  <pre><code>-(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];
  }
  </code></pre>
  
  <p>and the cancel delegate</p>
  
  <pre><code>-(void)imagePickerControllerDidCancel:(UIImagePickerController *)  picker
  {
  [picker dismissModalViewControllerAnimated:YES];
  picker = nil;
  [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
  
  }
  </code></pre>
  
  <p>Here are the sliders' delegate methods. Again, the NSLogs display the expected values.</p>
  
  <pre><code>- (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]];
  
  }
  </code></pre>
  
  <p>And finally, here is the doImageFilter method I created, which actually <em>does</em> the filter and reassigns the UIImageView (this is where it turns white)</p>
  
  <pre><code>- (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];
  
  }
  </code></pre>
  
  <p>So I have a hunch that the CIImage isn't even being initialized properly in the first place; but I don't know <em>why</em>. Been at this for a couple hours now.</p>
  
  <p>Thanks for all your help!</p>
  
  <p>Cheers</p>