简体   繁体   English

将方形UIImage裁剪为圆角矩形

[英]Crop Square UIImage into Rounded Rect

I have tried at least 5 different ways that I've found on here, but nothing has worked. 我尝试了至少5种在这里找到的不同方法,但是没有任何效果。 All are at least 1-2 years old minimum. 所有年龄都至少为1-2岁。 Has Apple changed something in the rules since then? 从那以后,Apple是否更改了规则中的某些内容?

I take a screenshot, crop it to a square, and then I am trying to crop its corners to a radius of 15. 我截取屏幕截图,将其裁剪为正方形,然后尝试将其角裁剪为半径15。

Maybe there is a more serious and certain way to do this with OpenGL? 也许使用OpenGL有更严肃和确定的方法可以做到这一点?

Trying this solution currently, but the corners still refuse to crop: 目前正在尝试此解决方案,但仍然无法解决:

-(UIImage*) cropImage:(UIImage*)image withPath:(UIBezierPath*)path { // where the UIBezierPath is defined in the UIKit coordinate system (0,0) is top left

    CGRect r = CGPathGetBoundingBox(path.CGPath); // the rect to draw our image in (minimum rect that the path occupies).

    UIGraphicsBeginImageContextWithOptions(r.size, NO, 0.0); // begin image context, with transparency & the scale of the image.
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextTranslateCTM(ctx, -r.origin.x, -r.origin.y); // translate context so that when we add the path, it starts at (0,0).

    CGContextAddPath(ctx, path.CGPath); // add path.
    CGContextClip(ctx); // clip any future drawing to the path region.

    [image drawInRect:(CGRect){CGPointZero, image.size}]; // draw image

    UIImage* i = UIGraphicsGetImageFromCurrentImageContext(); // get image from context
    UIGraphicsEndImageContext(); // clean up and finish context

    return i; // return image
}

Just to clarify, I'm trying to actually delete the pixels I crop 为了澄清,我正在尝试删除我裁剪的像素

And I'm passing in... 我在传递...

[self cropImage:myImage withPath:[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, screenImage.size.width, screenImage.size.height) cornerRadius:15.0]]

Even if I try greatly increases the corner radius, still nothing happens. 即使我尝试大大增加拐角半径,也仍然没有任何反应。

At this point you have probably found some solution out there, but for the next ones that will arrive here this solution here might help. 在这一点上,您可能已经找到了一些解决方案,但是对于将在此处到达的下一个解决方案,这里的解决方案可能会有所帮助。 It a bit of a workaround, since it uses a UIImageView to do the drawing job, but it works pretty well. 这是一种解决方法,因为它使用UIImageView来完成绘图工作,但是效果很好。

extension UIImage {

    var circle: UIImage? {
        let length = min(size.width, size.height)
        let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: length, height: length)))
        imageView.contentMode = .scaleAspectFill
        imageView.image = self
        imageView.layer.cornerRadius = length/2
        imageView.layer.masksToBounds = true
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        imageView.layer.render(in: context)
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    }
}

If you want a more robust solution, this one here might help: https://github.com/giacgbj/UIImageSwiftExtensions/ 如果您想要一个更强大的解决方案,那么这里的解决方案可能会有所帮助: https : //github.com/giacgbj/UIImageSwiftExtensions/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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