How to sharply / blur uiimage in iphone?

I have a view with UIImageViewand UIImage. How to make an image sharp or blurry with coregraphics?

+3
source share
4 answers

Apple has an excellent sample program called GLImageProcessing , which includes a very fast blur / sharpen effect using OpenGL ES 1.1 (which means that it works on all iPhone, not just 3gs.)

If you are not good enough in OpenGL, the code can hurt you.

+6
source

OpenGL ( ). , , , , , , . . :


- (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;
}
+6

API CoreImage. , CoreImage iPhone ( , ). , , IIRC, SIM-, .

AFAIK , , , , , . , tho, , , Photoshop .

, , , , .

0

Source: https://habr.com/ru/post/1718684/


All Articles