对于 iOS 6.1,我想要一个视图来封装标题序列的某些动态图形中使用的向内收缩和去模糊效果。我生成的代码(此处未全部显示)稳步降低拉伸因子(向内缩小文本的水平比例)和模糊量。应用此效果的文本呈现为 UIBezierPath 并存储在 self.myPath 中。计时器触发减少这两个值的方法并调用 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
}
我还没有检查这个代码是否有泄漏,所以认为它是“伪代码”:-) 如图所示,这可以在 drawRect: 中完成,而不是使用层,但我还有其他事情在这个视图中没有显示精简版。
但由于 CIGaussianBlur 需要大量时间,我正在研究使用 Accelerate 框架进行图像处理,以了解如何使我的版本更加流畅。