Skip to content

Instantly share code, notes, and snippets.

@HeidiHansen
Last active August 29, 2015 14:06
Show Gist options
  • Save HeidiHansen/06653614e0e0185663f5 to your computer and use it in GitHub Desktop.
Save HeidiHansen/06653614e0e0185663f5 to your computer and use it in GitHub Desktop.
Image Filter View Controller
#import "FISViewController.h"
@interface FISViewController ()
@property(strong, nonatomic) CIContext *context;
@property(strong, nonatomic) CIFilter *filter;
@property(strong, nonatomic) CIImage *beginImage;
@property(strong, nonatomic) NSMutableArray *arrayOfFilters;
@property(strong, nonatomic) NSString *filtersName;
@end
@implementation FISViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.theImageView.image = self.imageToProcess;
self.filterNamesPicker.delegate=self;
self.filterNamesPicker.dataSource =self;
FISFilterObject *oneObject = [[FISFilterObject alloc] initWithName:@"CISepiaTone"];
FISFilterObject *twoObject = [[FISFilterObject alloc] initWithName:@"CIToneCurve"];
FISFilterObject *threeObject = [[FISFilterObject alloc] initWithName:@"CIColorMatrix"];
FISFilterObject *fourObject = [[FISFilterObject alloc] initWithName:@"CIHueAdjust"];
FISFilterObject *fiveObject = [[FISFilterObject alloc] initWithName:@"CIDotScreen"];
FISFilterObject *sixObject = [[FISFilterObject alloc] initWithName:@"CIHoleDistortion"];
FISFilterObject *sevenObject = [[FISFilterObject alloc] initWithName:@"CIGaussianBlur"];
FISFilterObject *eightObject = [[FISFilterObject alloc] initWithName:@"Mutiple Filters"];
FISFilterObject *nineObject = [[FISFilterObject alloc] initWithName:@"CIGammaAdjust"];
NSArray *filters =@[oneObject, twoObject, threeObject, fourObject, fiveObject, sixObject, sevenObject,eightObject, nineObject];
self.arrayOfFilters = [[NSMutableArray alloc] initWithArray:filters];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [self.arrayOfFilters count];
//NSLog(@"The number of filters %d", [self.arrayOfFilters count]);
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
FISFilterObject *theObject= self.arrayOfFilters[row];
// NSLog(@"the row number is %d", row );
// NSLog(@"The filter Name is %@", theObject.filterName);
return theObject.filterName;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
FISFilterObject *filters = self.arrayOfFilters[row];
self.filtersName = filters.filterName;
}
- (IBAction)sliderEffect:(id)sender {
UISlider *mySlider = (UISlider *)sender;
float theValue = mySlider.value;
self.context = [CIContext contextWithOptions:nil];
NSInteger number = [self.filterNamesPicker selectedRowInComponent:0];
FISFilterObject *temp= self.arrayOfFilters[number];
NSLog(@"Filter Name %@", temp.filterName);
if ([self.filtersName isEqualToString:@"CISepiaTone" ]) {
self.beginImage = [[CIImage alloc] initWithImage: self.imageToProcess];
CIFilter * blurFilter = [CIFilter filterWithName:self.filtersName];
[blurFilter setValue:self.beginImage forKey:kCIInputImageKey];
[blurFilter setValue:@(theValue) forKey:@"inputIntensity"];
CGImageRef ref = [self.context createCGImage:blurFilter.outputImage fromRect:[blurFilter.outputImage extent]];
self.theImageView.image = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
}else if ([self.filtersName isEqualToString:@"CIToneCurve"]){
self.beginImage = [[CIImage alloc] initWithImage: self.imageToProcess];
CIFilter * blurFilter = [CIFilter filterWithName:self.filtersName];
[blurFilter setDefaults];
[blurFilter setValue:self.beginImage forKey:kCIInputImageKey];
[blurFilter setValue:[CIVector vectorWithX:0.0 Y:0.0] forKey:@"inputPoint0"]; // default
[blurFilter setValue:[CIVector vectorWithX:0.27 Y:0.26] forKey:@"inputPoint1"];
[blurFilter setValue:[CIVector vectorWithX:theValue Y:0.80] forKey:@"inputPoint2"];
[blurFilter setValue:[CIVector vectorWithX:0.7 Y:1.0] forKey:@"inputPoint3"];
[blurFilter setValue:[CIVector vectorWithX:1.0 Y:theValue] forKey:@"inputPoint4"]; // default
CGImageRef ref = [self.context createCGImage:blurFilter.outputImage fromRect:[blurFilter.outputImage extent]];
self.theImageView.image = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
}else if ([self.filtersName isEqualToString:@"CIColorMatrix"]){
self.beginImage = [[CIImage alloc] initWithImage: self.imageToProcess];
CIFilter *filterColorMatrix = [CIFilter filterWithName:@"CIColorMatrix"];
[filterColorMatrix setValue:self.beginImage forKey:kCIInputImageKey];
CIVector *greenVector = [CIVector vectorWithX:mySlider.value Y:mySlider.value Z:0 W:0];
[filterColorMatrix setValue:greenVector forKey:@"inputGVector"];
CGImageRef ref = [self.context createCGImage:filterColorMatrix.outputImage fromRect:[filterColorMatrix.outputImage extent]];
//
self.theImageView.image = [UIImage imageWithCGImage:ref];
//
CGImageRelease(ref);
}else if ([self.filtersName isEqualToString:@"CIHueAdjust"]){
self.beginImage = [[CIImage alloc] initWithImage: self.imageToProcess];
CIFilter * blurFilter = [CIFilter filterWithName:self.filtersName];
[blurFilter setValue:self.beginImage forKey:kCIInputImageKey];
[blurFilter setValue:[NSNumber numberWithDouble:-2*M_PI/theValue] forKey:@"inputAngle"];
CGImageRef ref = [self.context createCGImage:blurFilter.outputImage fromRect:[blurFilter.outputImage extent]];
self.theImageView.image = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
}else if ([self.filtersName isEqualToString:@"CIDotScreen"]){
self.beginImage = [[CIImage alloc] initWithImage: self.imageToProcess];
CIFilter * blurFilter = [CIFilter filterWithName:self.filtersName];
[blurFilter setValue:self.beginImage forKey:@"inputImage"];
[blurFilter setValue:[NSNumber numberWithFloat:theValue+20.00] forKey:@"inputWidth"];
[blurFilter setValue:[NSNumber numberWithFloat:theValue] forKey:@"inputAngle"];
[blurFilter setValue:[NSNumber numberWithFloat:0.70] forKey:@"inputSharpness"];
CGImageRef ref = [self.context createCGImage:blurFilter.outputImage fromRect:[blurFilter.outputImage extent]];
self.theImageView.image = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
}else if ([self.filtersName isEqualToString:@"CIHoleDistortion"]){
self.beginImage = [[CIImage alloc] initWithImage: self.imageToProcess];
CIFilter * blurFilter = [CIFilter filterWithName:self.filtersName];
[blurFilter setValue:self.beginImage forKey:@"inputImage"];
[blurFilter setValue:[CIVector vectorWithX:150 Y:150] forKey:@"inputCenter"];
[blurFilter setValue:[NSNumber numberWithFloat:theValue +100] forKey:@"inputRadius"];
CGImageRef ref = [self.context createCGImage:blurFilter.outputImage fromRect:[blurFilter.outputImage extent]];
self.theImageView.image = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
}else if ([self.filtersName isEqualToString:@"CIGaussianBlur"]){
self.beginImage = [[CIImage alloc] initWithImage: self.imageToProcess];
CIFilter * blurFilter = [CIFilter filterWithName:self.filtersName];
blurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[blurFilter setValue:self.beginImage forKey: @"inputImage"];
[blurFilter setValue:[NSNumber numberWithFloat: theValue +1] forKey: @"inputRadius"];
CIImage *resultImage = [blurFilter valueForKey: @"outputImage"];
CGImageRef ref = [self.context createCGImage:resultImage fromRect:[resultImage extent]];
self.theImageView.image = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
}else if ([self.filtersName isEqualToString:@"Mutiple Filters"]){
self.beginImage = [[CIImage alloc] initWithImage: self.imageToProcess];
CIFilter *filterColorMatrix = [CIFilter filterWithName:@"CIColorMatrix"];
[filterColorMatrix setValue:self.beginImage forKey:kCIInputImageKey];
CIVector *greenVector = [CIVector vectorWithX:1 Y:theValue Z:0 W:0];
[filterColorMatrix setValue:greenVector forKey:@"inputGVector"];
CIImage *result = [filterColorMatrix outputImage];
CIFilter *filterBloom = [CIFilter filterWithName:@"CIBloom"];
[filterBloom setValue:result forKey:kCIInputImageKey];
[filterBloom setValue:[NSNumber numberWithDouble:5.0] forKey:@"inputRadius"];
[filterBloom setValue:[NSNumber numberWithDouble:2.0] forKey:@"inputIntensity"];
result = [filterBloom valueForKey:kCIOutputImageKey];
CGImageRef ref = [self.context createCGImage:result fromRect:[result extent]];
self.theImageView.image = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
}else if ([self.filtersName isEqualToString:@"CIGammaAdjust"]){
self.beginImage = [[CIImage alloc] initWithImage: self.imageToProcess];
CIFilter * blurFilter = [CIFilter filterWithName:self.filtersName];
[blurFilter setValue:self.beginImage forKey:@"inputImage"];
[blurFilter setValue:[NSNumber numberWithFloat:theValue+1] forKey:@"inputPower"];
CGImageRef ref = [self.context createCGImage:blurFilter.outputImage fromRect:[blurFilter.outputImage extent]];
self.theImageView.image = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
}else{
return;
}
}
- (IBAction)saveButtonTapped:(id)sender {
self.savedImage = self.theImageView.image;
[self.delegate setFilteredImage:self.savedImage];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
@HeidiHansen
Copy link
Author

Cowritten with Steve Zhou

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment