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.
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];
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With