简体   繁体   中英

How to get UIBezierPath of shape in UIImage or crop UIImage in a certain shape

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 UIImage s?, but make sure cropping is in a custom shape (like: face, heart etc), not in a rectangle.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM