The procedure for using your own custom filter is the same as the procedure for using any filter provided by Core Image except that you must initialize the filter class. You initialize the haze removal filter class created in the last section with this line of code:
[MyHazeFilter class]; |
Listing 3-8 shows how to use the haze removal filter. Note the similarity between this code and the code discussed in “Processing an Image.”
Note: If you’ve packaged your filter as an image unit, you need to load it. See “Using Core Image Filters” for details.
Listing 3-8 Using your own custom filter
- (void)drawRect: (NSRect)rect |
{ |
CGRect cg = CGRectMake(NSMinX(rect), NSMinY(rect), |
NSWidth(rect), NSHeight(rect)); |
CIContext *context = [[NSGraphicsContext currentContext] CIContext]; |
if(filter == nil) |
{ |
NSURL *url; |
[MyHazeFilter class]; |
url = [NSURL fileURLWithPath: [[NSBundle mainBundle] |
pathForResource: @"CraterLake" ofType: @"jpg"]]; |
filter = [CIFilter filterWithName: @"MyHazeRemover" |
keysAndValues: @"inputImage", |
[CIImage imageWithContentsOfURL: url], |
@"inputColor", |
[CIColor colorWithRed:0.7 green:0.9 blue:1], |
nil]; |
[filter retain]; |
} |
[filter setValue: [NSNumber numberWithFloat: distance] |
forKey: @"inputDistance"]; |
[filter setValue: [NSNumber numberWithFloat: slope] |
forKey: @"inputSlope"]; |
[context drawImage: [filter valueForKey: @"outputImage"] |
atPoint: cg.origin fromRect: cg]; |
} |
Last updated: 2008-06-09