2012-10-25 94 views
0

在我的项目中,我必须制作屏幕截图并应用模糊来创建磨砂玻璃效果。内容可以在玻璃下移动,然后调用bluredImageWithRect方法。我正试图优化下面的方法来加速应用程序。将模糊滤镜应用于屏幕截图时会发生重大损失,因此我正在寻找以较低分辨率拍摄屏幕截图的方法,对截图应用模糊处理,然后对其进行拉伸以适合某些矩形。截图低质量iOS

- (CIImage *)bluredImageWithRect:(CGRect)rect { 

    CGSize smallSize = CGSizeMake(rect.size.width, rect.size.height); 

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef ctx = CGBitmapContextCreate(nil, smallSize.width, smallSize.height, 8, 0, colorSpaceRef, kCGImageAlphaPremultipliedFirst); 
    CGContextClearRect(ctx, rect); 
    CGColorSpaceRelease(colorSpaceRef); 
    CGContextSetInterpolationQuality(ctx, kCGInterpolationNone); 
    CGContextSetShouldAntialias(ctx, NO); 
    CGContextSetAllowsAntialiasing(ctx, NO); 

    CGContextTranslateCTM(ctx, 0.0, self.view.frame.size.height); 
    CGContextScaleCTM(ctx, 1, -1); 

    CGImageRef maskImage = [UIImage imageNamed:@"mask.png"].CGImage; 
    CGContextClipToMask(ctx, rect, maskImage); 


    [self.view.layer renderInContext:ctx]; 

    CGImageRef imageRef1 = CGBitmapContextCreateImage(ctx); 

    CGContextRelease(ctx); 

    NSDictionary *options = @{(id)kCIImageColorSpace : (id)kCFNull}; 
    CIImage *beforeFilterImage = [CIImage imageWithCGImage:imageRef1 options:options]; 

    CGImageRelease(imageRef1); 

    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur" keysAndValues:kCIInputImageKey, beforeFilterImage, @"inputRadius", [NSNumber numberWithFloat:3.0f], nil]; 
    CIImage *afterFilterImage = blurFilter.outputImage; 

    CIImage *croppedImage = [afterFilterImage imageByCroppingToRect:CGRectMake(0, 0, smallSize.width, smallSize.height)]; 

    return croppedImage; 
} 

回答

2

这里是一个教程iOS image processing with the accelerate framework显示了如何做一个模糊的效果,可能是速度不够快,你所需要的。

+0

非常感谢!工作速度比CIFilter解决方案快得多。它现在仍然使图片颜色与alpha,我试图改变色彩空间到CGColorSpaceCreateDeviceRGB和CGBitmapInfo bitmapInfo kCGImageAlphaPremultipliedFirst,但现在我有一个错误::CGBitmapContextCreate:无效的数据字节/行:应该是至少1280 8整数位/组件,3个组件。创建上下文时,能否帮助我选择正确的设置? – Numeral

+0

我试了一下,没有多少运气。要创建CGContextRef,您需要使用kCGImageAlphaNoneSkipFirst。 – EarlyRiser

+0

最后做了这个使用从这里的例子:http://indieambitions.com/idevblogaday/perform-b​​lur-vimage-accelerate-framework-tutorial/ – Numeral