2012-10-25 96 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