开发者

How to clip UIImageView in different shape

开发者 https://www.devze.com 2023-03-02 20:55 出处:网络
Hi all : I create a UIImageView with my photo on my iphone sc开发者_运维知识库reen just now, its shape is rectangle. and I want to clip the UIImageView into the shape of diamond. if there is any part

Hi all : I create a UIImageView with my photo on my iphone sc开发者_运维知识库reen just now, its shape is rectangle. and I want to clip the UIImageView into the shape of diamond. if there is any part of my photo beyond the diamond area, don't display it.

But now I don't quirt understand how to do it, so I'm ask for help. thanks !


You try to add mask to your image using method below and put it to UIImageView

+ (UIImage *)image:(UIImage *)img withMask:(UIImage *)maskImg {
CGImageRef maskRef = maskImg.CGImage; 

CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                    CGImageGetHeight(maskRef),
                                    CGImageGetBitsPerComponent(maskRef),
                                    CGImageGetBitsPerPixel(maskRef),
                                    CGImageGetBytesPerRow(maskRef),
                                    CGImageGetDataProvider(maskRef), NULL, false);

CGImageRef masked = CGImageCreateWithMask(img.CGImage, mask);
UIImage *image = [UIImage imageWithCGImage:masked];
CGImageRelease(mask);
CGImageRelease(masked);
return image;

}

0

精彩评论

暂无评论...
验证码 换一张
取 消