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.
- have you acheived this ? I need similar functionality but below code is not working.Rahul– Rahul2017-09-16 06:49:13 +00:00Commented Sep 16, 2017 at 6:49
Add a comment |
1 Answer
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]; 10 Comments
Syed Sharjeel Ali
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.
Keenle
Oh, so you have a mask image and want to use to crop another image?
Syed Sharjeel Ali
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.
Keenle
Before
cerateImageFromImage:withImageMask: method call try to put break point and verify that you are passing non nil valies.Syed Sharjeel Ali
I also try this all both the images have value but when the code pass to the "imageMask", it give "nill" value.
|