Create faded corners / sides of UIImage (not animation)

I would like to know how I can make the fading angle / side of a UIImage as an attached image.

I set it as a layer and containing round corners as shown below, but what code can I fade out or the gradient?

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

}

Many thanks.

enter image description here

+6
source share
2 answers

To expand the response, create a mask with a completely opaque center, and the edges should be shifted towards transparency, and then use the 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]; } 
+6
source

If the size of your image is always the same (or the aspect ratio remains the same), you are probably best at implementing this with the CALayer mask.

In anticipation of the time, you will create a mask image (in Photoshop or a similar tool) that indicates that the central square of the image should be completely opaque and so that the edges are mixed with transparency.

In the code, you upload this image and create a CALayer mask, which you then use to mask the original image.

Sorry, I do not have any code for this, but it can get you started!

+3
source

Source: https://habr.com/ru/post/906185/


All Articles