iOS圖像模糊處理
先添加CoreGraphics.framework
代碼如下:
- (UIImage) blur:(UIImage)theImage
{
// create our blurred image
CIContext context = [CIContext contextWithOptions:nil];
CIImage inputImage = [CIImage imageWithCGImage:theImage.CGImage];// setting up Gaussian Blur (could use one of many filters offered by Core Image) CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"]; [blurFilter setValue:inputImage forKey:kCIInputImageKey]; [blurFilter setValue:[NSNumber numberWithFloat:10.0f] forKey:@"inputRadius"]; CIImage *result = [blurFilter valueForKey:kCIOutputImageKey]; // CIGaussianBlur has a tendency to shrink the image a little, // this ensures it matches up exactly to the bounds of our original image CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]]; UIImage *returnImage = [UIImage imageWithCGImage:cgImage]; //create a UIImage for this function to "return" so that ARC can manage the memory of the blur... //ARC can't manage CGImageRefs so we need to release it before this function "returns" and ends. CGImageRelease(cgImage);//release CGImageRef because ARC doesn't manage this on its own. return returnImage; } </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959613894704408382" target="_blank"></a></div>
</div> </div> 出現問題:因為在模糊的時候,邊緣會變成半透明的狀態,所以理想狀況是可以對原圖像進行適當放大,選擇使用CIAffineClamp在模糊之前對圖像進行處理。
源代碼如下:
- (UIImage) blur:(UIImage)theImage
{
// create our blurred image
CIContext context = [CIContext contextWithOptions:nil];
CIImage inputImage = [CIImage imageWithCGImage:theImage.CGImage];CIFilter *affineClampFilter = [CIFilter filterWithName:@"CIAffineClamp"]; CGAffineTransform xform = CGAffineTransformMakeScale(1.0, 1.0); [affineClampFilter setValue:inputImage forKey:kCIInputImageKey]; [affineClampFilter setValue:[NSValue valueWithBytes:&xform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"]; CIImage *extendedImage = [affineClampFilter valueForKey:kCIOutputImageKey]; // setting up Gaussian Blur (could use one of many filters offered by Core Image) CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"]; [blurFilter setValue:extendedImage forKey:kCIInputImageKey]; [blurFilter setValue:[NSNumber numberWithFloat:10.0f] forKey:@"inputRadius"]; CIImage *result = [blurFilter valueForKey:kCIOutputImageKey]; // CIGaussianBlur has a tendency to shrink the image a little, // this ensures it matches up exactly to the bounds of our original image CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]]; UIImage *returnImage = [UIImage imageWithCGImage:cgImage]; //create a UIImage for this function to "return" so that ARC can manage the memory of the blur... //ARC can't manage CGImageRefs so we need to release it before this function "returns" and ends. CGImageRelease(cgImage);//release CGImageRef because ARC doesn't manage this on its own. return returnImage; } </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959613894704408382" target="_blank"></a></div>
</div> </div>
參考:(網上資源,部分鏈接已丟失)http://stackoverflow.com/questions/12839729/correct-crop-of-cigaussianblur本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!