I have tried at least 5 different ways that I've found on here, but nothing has worked. All are at least 1-2 years old minimum. Has Apple changed something in the rules since then?
I take a screenshot, crop it to a square, and then I am trying to crop its corners to a radius of 15.
Maybe there is a more serious and certain way to do this with 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.
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/
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.