Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a faded corners/sides on UIImage (not animation)

Tags:

iphone

uiimage

I would like to know how can i make a fading corner/side of a UIImage as the attached picture.

I have set it as a layer and containing round corners as below, but what's the code i can fade or gradient it?

-(void) viewDidLoad{
[super viewDidLoad];
CALayer * layer = [image layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:10.0];
}

Thank you very much.

enter image description here

like image 726
Clarence Avatar asked Jan 17 '12 08:01

Clarence


2 Answers

Extending the above answer create mask with fully opaque center and edges should blend towards transparency then use following code to mask

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

    CGImageRef imgRef = [image CGImage];
    CGImageRef maskRef = [maskImage CGImage];
    CGImageRef actualMask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                              CGImageGetHeight(maskRef),
                                              CGImageGetBitsPerComponent(maskRef),
                                              CGImageGetBitsPerPixel(maskRef),
                                              CGImageGetBytesPerRow(maskRef),
                                              CGImageGetDataProvider(maskRef), NULL, false);
    CGImageRef masked = CGImageCreateWithMask(imgRef, actualMask);
    return [UIImage imageWithCGImage:masked];

}
like image 109
abdus.me Avatar answered Oct 19 '22 21:10

abdus.me


If the size of your image is always the same (or the aspect ratio stays the same) then you're probably best implementing this using a CALayer mask.

Ahead of time you would create a mask image (in photoshop or similar tool) which specifies that the center square of the image should be fully opaque and that the edges should blend towards transparency.

In code you'd load this image and create a CALayer mask which you then use to mask the original image.

I'm sorry I don't have any code handy for this but it might get you started!

like image 43
gdawg Avatar answered Oct 19 '22 21:10

gdawg