Search code examples
iosobjective-cuiimageviewuiimage

Particular shape image on uiimageview


Can we set below shape on image with UIImageView.

Any idea!

Thanks

enter image description here


Solution

  • Yes, you can set a shape on UIImage. First, add a Mask image(which shape you want) in your project. and write these code.

    [yourImageView setImage:[self createMaskImage:yourImage withMask:MaskImage]];
    
    - (UIImage*) createMaskImage:(UIImage *)image withMask:(UIImage *)maskImage {
        CGImageRef maskRef = maskImage.CGImage;
    
        CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                            CGImageGetHeight(maskRef),
                                            CGImageGetBitsPerComponent(maskRef),
                                            CGImageGetBitsPerPixel(maskRef),
                                            CGImageGetBytesPerRow(maskRef),
                                            CGImageGetDataProvider(maskRef), NULL, false);
    
        CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
        return [UIImage imageWithCGImage:masked];
    }