简体   繁体   中英

Crop an image using UIBezierPath - Beginner

I have a shape defined in UIBezierPath . I also have an UIImage . I now want to crop this image using UIBezierPath . How can I do this ?

let shape = UIBezierPath(rect: imageRect)

let image = UIImage(cgimage: c)

I am putting Objective C code. However it is easy to convert in swift3

Please try below code

CAShapeLayer *newLayer = [[CAShapeLayer alloc] init];
newLayer.path = self.shape.CGPath;


[self.view.layer setMask:newLayer];

CGRect rectToCrop = self.shape.bounds;

UIGraphicsBeginImageContext(self.view.frame.size);//, NO, 0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[newLayer removeFromSuperlayer];
newLayer = nil;

CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rectToCrop);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:self.imgVPhoto.image.scale orientation:self.imgVPhoto.image.imageOrientation];
CGImageRelease(imageRef);

And then save

[[NSOperationQueue mainQueue] addOperationWithBlock:^{

    //saving cut Image
    NSData *imgData = UIImagePNGRepresentation(croppedImage);

    if (imgData) {

        NSString *imagePath = 

        if([FileUtility fileExistsAtFilePath:imagePath]) {
            [FileUtility deleteFile:imagePath];
        }

        //need to add this task in BG to avoid blocking of main thread

        [imgData writeToFile:imagePath atomically:true];

    }

}];

Hope it helps to you

EDIT

SWIFT Convert

var newLayer = CAShapeLayer()
newLayer.path = shape.cgPath
view.layer.mask = newLayer
var rectToCrop: CGRect = shape.bounds
UIGraphicsBeginImageContext(view.frame.size)
//, NO, 0);
view.layer.render(inContext: UIGraphicsGetCurrentContext())
var image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
newLayer.removeFromSuperlayer()
newLayer = nil
var imageRef: CGImageRef? = image?.cgImage.cropping(to: rectToCrop)
var croppedImage = UIImage(cgImage: imageRef!, scale: imgVPhoto.image.scale, orientation: imgVPhoto.image.imageOrientation)
CGImageRelease(imageRef)

If you are displaying the image in a UIImageView, you can use the mask property.

You need to make a UIImage out of the path:

extension UIBezierPath {
    func asMaskImage(fillColor:UIColor = UIColor.black) -> UIImage
    {
        // Make a graphics context
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
        let context = UIGraphicsGetCurrentContext()

        context!.setFillColor(fillColor.cgColor)

        // and fill it!
        self.fill()

        let image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        return image!
     }
}

Then you use that as the mask:

myImageView.image = image
myImageView.mask = UIImageView(image: shape.asMaskImage())

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