问题
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
回答1:
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
回答2:
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 :)
来源:https://stackoverflow.com/questions/17004812/ios-invert-a-uiimage-with-button