1

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
Milo
  • 5,041
  • 7
  • 33
  • 59
  • http://stackoverflow.com/questions/6672517/is-programmatically-inverting-the-colors-of-an-image-possible – thelaws Jun 08 '13 at 23:10
  • How do I implement that code in my situation? – Milo Jun 08 '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](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html) – thelaws Jun 08 '13 at 23:30
  • Thank you. I will review the link you gave me. – Milo Jun 08 '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 09 '13 at 03:35
  • I found code that does that pixel by pixel and it is working exactly as you expect http://stackoverflow.com/questions/24049313/how-do-i-load-and-edit-a-bitmap-file-at-the-pixel-level-in-swift-for-ios#24139701 – Khaled Annajar Oct 12 '16 at 09:43

2 Answers2

9

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

Adithya
  • 4,545
  • 3
  • 25
  • 28
0

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 :)

Charles D'Monte
  • 370
  • 4
  • 16