Friday, 24 February 2012

Bluring to imageview + iphone

-(UIImage*)imageWithBlurAroundPoint:(CGPoint)point {
    CGRect             bnds = CGRectZero;
    UIImage*           copy = nil;
    CGContextRef       ctxt = nil;
    CGImageRef         imag = rgbImg.image.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(rgbImg.image.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;

}

No comments:

Post a Comment