4

I am new in iOS, I want to know if I can get the UIBezierPath of a UIImage. I have a UIImage of face layout and want to get the UIBezierPath, which helps me in cropping the UIImage. Or, can any one tell me about other ways of cropping UIImages?, but make sure cropping is in a custom shape (like: face, heart etc), not in a rectangle.

1
  • have you acheived this ? I need similar functionality but below code is not working. Commented Sep 16, 2017 at 6:49

1 Answer 1

4

Here is the code to mask image with image:

- (UIImage *)cerateImageFromImage:(UIImage *)image withMaskImage:(UIImage *)mask { CGImageRef imageRef = image.CGImage; CGImageRef maskRef = mask.CGImage; CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(maskRef), CGImageGetHeight(maskRef), CGImageGetBitsPerComponent(maskRef), CGImageGetBitsPerPixel(maskRef), CGImageGetBytesPerRow(maskRef), CGImageGetDataProvider(maskRef), NULL, YES); CGImageRef maskedReference = CGImageCreateWithMask(imageRef, imageMask); CGImageRelease(imageMask); UIImage *maskedImage = [UIImage imageWithCGImage:maskedReference]; CGImageRelease(maskedReference); return maskedImage; } 

Usage:

UIImage *image = [UIImage imageNamed:@"Photo.png"]; UIImage *mask = [UIImage imageNamed:@"Mask.png"]; self.imageView.image = [self cerateImageFromImage:image withMaskImage:mask]; 
Sign up to request clarification or add additional context in comments.

10 Comments

I am very thankful to you for such a quick response, but sorry to say i am unable to get desire result from this code. when i create image of face layout, it gives me an oval shape image which have some missing part from top on both side, left and right hand side.
Oh, so you have a mask image and want to use to crop another image?
Keenle, Unable to get the result, it returns me "nill" value. I already try this code, i don't know why all this happing. I use this code in this way: UIImage *image = [UIImage imageNamed:@"Some image from camera or person face"]; UIImage *mask = [UIImage imageNamed:@"face layout image"]; and then call the method.
Before cerateImageFromImage:withImageMask: method call try to put break point and verify that you are passing non nil valies.
I also try this all both the images have value but when the code pass to the "imageMask", it give "nill" value.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.