如何锐化/模糊uiimage在iphone? [英] how to sharp/blur an uiimage in iphone?

查看:360
本文介绍了如何锐化/模糊uiimage在iphone?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图,其中设置了 UIImageView UIImage 。如何使用核心图像使图像清晰或模糊?

I have a view with UIImageView and an UIImage set to it. How do I make image sharp or blur using coregraphics?

推荐答案

走下去OpenGL路线感觉像我疯狂的凌辱图像上的触摸点)。相反,我实现了一个简单的模糊过程,需要一个接触点,创建一个矩形包含该接触点,对该点的图像进行采样,然后重新绘制样本图像上下颠倒在源矩形几次轻微偏移稍有不同的不透明度。这产生了一个相当不错的穷人的模糊效果,没有疯狂的代码量和复杂性。代码如下:

Going down the OpenGL route felt like insane overkill for my needs (blurring a touched point on an image). Instead I implemented a simple blurring process that takes a touch point, creates a rect containing that touch point, samples the image in that point and then redraws the sample image upside down on top of the source rect several times slightly offset with slightly different opacity. This produces a pretty nice poor man's blur effect without an insane amount of code and complexity. Code follows:

- (UIImage*)imageWithBlurAroundPoint:(CGPoint)point {
    CGRect             bnds = CGRectZero;
    UIImage*           copy = nil;
    CGContextRef       ctxt = nil;
    CGImageRef         imag = self.CGImage;
    CGRect             rect = CGRectZero;
    CGAffineTransform  tran = CGAffineTransformIdentity;
    int                indx = 0;

    rect.size.width  = CGImageGetWidth(imag);
    rect.size.height = CGImageGetHeight(imag);

    bnds = rect;

    UIGraphicsBeginImageContext(bnds.size);
    ctxt = UIGraphicsGetCurrentContext();

    // Cut out a sample out the image
    CGRect fillRect = CGRectMake(point.x - 10, point.y - 10, 20, 20);
    CGImageRef sampleImageRef = CGImageCreateWithImageInRect(self.CGImage, fillRect);

    // Flip the image right side up & draw
    CGContextSaveGState(ctxt);

    CGContextScaleCTM(ctxt, 1.0, -1.0);
    CGContextTranslateCTM(ctxt, 0.0, -rect.size.height);
    CGContextConcatCTM(ctxt, tran);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), rect, imag);

    // Restore the context so that the coordinate system is restored
    CGContextRestoreGState(ctxt);

    // Cut out a sample image and redraw it over the source rect
    // several times, shifting the opacity and the positioning slightly
    // to produce a blurred effect
    for (indx = 0; indx < 5; indx++) {
        CGRect myRect = CGRectOffset(fillRect, 0.5 * indx, 0.5 * indx);
        CGContextSetAlpha(ctxt, 0.2 * indx);
        CGContextScaleCTM(ctxt, 1.0, -1.0);
        CGContextDrawImage(ctxt, myRect, sampleImageRef);
    }

    copy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return copy;
}

这篇关于如何锐化/模糊uiimage在iphone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆