views:

1035

answers:

3

i have a view with uiimageview and an UIimage set to it.how do i make image sharp or blur using coregraphics?

A: 

What you really need are in the image filters in the CoreImage API. Unfortunately CoreImage is not supported on the iPhone (unless that changed recently and I missed it). Be careful here, as, IIRC, they are available in the SIM - but not on the device.

AFAIK there is no other way to do it properly with the native libraries, although I've sort of faked a blur before by creating an extra layer over the top which is a copy of what's below, offset by a pixel or two and with a low alpha value. For a proper blur effect, tho, the only way I've been able to do it is offline in Photoshop or similar.

Would be keen to hear if there is a better way too, but to my knowledge that is the situation currently.

Phil Nash
+3  A: 

Apple has a great sample program called GLImageProcessing that includes a very fast blur/sharpen effect using OpenGL ES 1.1 (meaning it works on all iPhones, not just the 3gs.)

If you're not fairly experienced with OpenGL, the code may make your head hurt.

David Maymudes
A: 

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;

}

Blake Watters