For iOS 6.1 I wanted a view that encapsulated the shrinking inward and deblurring effect used in some motion graphics for title sequences. My resulting code (not all shown here) steadily decreases both the stretch factor (inwardly shrinking the horizontal scale of the text) and the blur amount. The text against which this effect is applied is rendered as a UIBezierPath and stored in self.myPath. A timer fires the method that decreases the two values and calls setNeedsDisplay.
- (void)displayLayer:(CALayer *)layer
{
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGAffineTransform stretch = CGAffineTransformMakeScale(self.stretchFactor + 1.0, 1.0);
CGPathRef stretchedPath = CGPathCreateCopyByTransformingPath([self.myPath CGPath], &stretch);
CGRect newBox = CGPathGetBoundingBox(stretchedPath);
float deltaX = CGRectGetMidX(self.bounds) - CGRectGetMidX(newBox);
float deltaY = CGRectGetMidY(self.bounds) - CGRectGetMidY(newBox);
CGAffineTransform slide = CGAffineTransformMakeTranslation(deltaX, deltaY);
CGPathRef centeredPath = CGPathCreateCopyByTransformingPath(stretchedPath, &slide);
CGPathRelease(stretchedPath);
CGContextAddPath(ctx, centeredPath);
CGPathRelease(centeredPath);
CGContextSetFillColorWithColor(ctx, [[UIColor blackColor] CGColor]);
CGContextFillPath(ctx);
UIImage *tmpImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CIImage *inputImage = [CIImage imageWithCGImage:[tmpImage CGImage]];
CIFilter *gBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"
keysAndValues:@"inputRadius", [NSNumber numberWithFloat:self.blurFactor],
@"inputImage", inputImage, nil];
CIImage *blurredImage = [gBlurFilter outputImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimg = [context createCGImage:blurredImage fromRect:[blurredImage extent]];
[layer setContents:(__bridge id)cgimg];
CGImageRelease(cgimg);
}
- (void)drawRect:(CGRect)rect
{
// empty drawRect: to get the attention of UIKit
}
I haven't yet checked this code for leaks, so consider it "pseudo code" :-) As shown this could have been done within drawRect: and not used layers, but I have other things going on with this view not shown in this condensed version.
But since CIGaussianBlur takes a noticeable amount of time, I'm looking at image processing using the Accelerate framework to see about making my version more fluid.