cannyboy
cannyboy

Reputation: 24416

Blur UIImageView with gradient alpha

How would I make an UIImageView have a gradient blurred background? In other words, the image is fully blurred on the left and it gets less blurred on the right?

Upvotes: 0

Views: 1319

Answers (1)

lucaslt89
lucaslt89

Reputation: 2451

we did the same in a proyect! we used this method:

(UIImage *)blurImage:(CGImageRef*)image withBlurLevel:(CGFloat)blur
{
    CIImage *inputImage = [CIImage imageWithCGImage:image];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"
                                  keysAndValues:kCIInputImageKey, inputImage, @"inputRadius", @(blur), nil];

    CIImage *outputImage = filter.outputImage;

    CIContext *context = [CIContext contextWithOptions:nil];

    CGImageRef outImage = [context createCGImage:outputImage
                                        fromRect:[inputImage extent]];

    UIImage *returnImage = [UIImage imageWithCGImage:outImage];

    CGImageRelease(outImage);

    return returnImage;
}

This code use your GPU to make the blur, but you need to call that method in another thread if you don't want your UI to be blocked for a second.

Hope it helps!

Upvotes: 4

Related Questions