Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a simple application where I can take an photo or choose and existing photo on the camera roll and then display the image in a UIImageView. I want to have a button that you can press and invert the picked image and then display that in the camera roll. My question is, how do invert the colors of a UIImage and then use the new inverted one to display in the ImageView? I have seen other posts related to this but none explain how implement the code. I'm fairly new to xcode and would appreciate all feedback.

Here is my .m file for the view controller:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize imageView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)takePhoto {
    picker1 = [[UIImagePickerController alloc] init];
    picker1.delegate = self;
    [picker1 setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentViewController:picker1 animated:YES completion:nil];

}

-(IBAction)ChooseExisting {

    picker2 = [[UIImagePickerController alloc] init];
    picker2.delegate = self;
    [picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:picker2 animated:YES completion:nil];

}

-(IBAction)invertImage {

}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    imageFinal = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageView setImage:imageFinal];
    [self dismissViewControllerAnimated:YES completion:nil];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
share|improve this question
    
    
How do I implement that code in my situation? –  Milo Jun 8 '13 at 23:16
    
It needs to be put in a different file, then use: self.imageView.image = [self.imageView.image negativeImage]; Go read about categories in objective-c –  thelaws Jun 8 '13 at 23:30
    
Thank you. I will review the link you gave me. –  Milo Jun 8 '13 at 23:32
    
Thank you so much thelaws! You really saved me. Now i believe I have a bit more understanding of xcode! Rep for you! –  Milo Jun 9 '13 at 3:35
add comment

2 Answers

I see the negetiveImage method has a lot of code. Here's a simple one using Core Image filters.

Include Core Image Framework to your project Import the core Image header.

#import <CoreImage/CoreImage.h>

The below code should return you the inverted UIImage

UIImage *inputImage = [UIImage imageNamed:@"imageName"];
CIFilter* filter = [CIFilter filterWithName:@"CIColorInvert"];
[filter setDefaults];
[filter setValue:inputImage.CIImage forKey:@"inputImage"];
UIImage *outputImage = [[UIImage alloc] initWithCIImage:filter.outputImage];

The outputImage will be inverted

share|improve this answer
2  
Tip: create CIImage with [[CIImage alloc] initWithCGImage:inputImage.CGImage] if inputImage.CIImage doesn't work for you.; –  Joseph Lin Jun 12 '13 at 21:59
add comment

To give you an easy answer, you can invert the UIImageView which has your UIImage.

To invert it, just use

[yourImageView setTransform:CGAffineTransformMakeScale(-1, 1)]; // flip horizontally
[yourImageView setTransform:CGAffineTransformMakeScale(1, -1)]; // flip vertically
[yourImageView setTransform:CGAffineTransformMakeScale(-1, -1)]; // flip horizontally and vertically

and to bring it back to normal, use

[yourImageView setTransform:CGAffineTransformIdentity]; // bring the view back to normal.

Hope this helps and I wish you all the best with you app :)

share|improve this answer
    
did you read the question? –  deepflame Aug 19 '13 at 12:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.