OdNairy
OdNairy

Reputation: 540

Mask Image to Round in iOS

I know there are many questions about mask but answers for them didn't help me (just partly).

I need to do my image round. I created mask.png and use it in code:

- (UIImage*) maskImage:(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);
UIImage* maskedImage = [UIImage imageWithCGImage:masked];

CGImageRelease(masked);
CGImageRelease(mask);
return maskedImage;
}

Everything was great and everybody was happy. But when I start test this method at device I found that it isn't mask my image (in simulator everything working ok). How I can mask my image?

P.S. Temporarily I seted view.layes.mask as layer with my mask. And this variant is working at the device. May be any way to save round from this layer?

Upvotes: 1

Views: 1074

Answers (1)

user4615157
user4615157

Reputation:

Its not required to do all those stuffs if all you needed is a round imageview.

UIImageView * myimageview = [[UIImageView alloc]init];
myimageview.layer.cornerRadius = myimageview.frame.size.height;
myimageview.layer.masksToBounds = true;

use the above code and set any image to that imageview and you will get a round imageview. here you're not making the image round, but the imageview.

Upvotes: 1

Related Questions