Writing in the front

We must do this demand, cut fillet for a picture, of course, most of us adopt the method of simple and crude myIcon. Layer. The cornerRadius = 16.5 myIcon. Layer. MasksToBounds =trueIt doesn't matter if it's a static page, but if it's a slide-able page with lots of images to crop, then performance matters. And this is one of the good ones, This code has been uploaded to githup [DDGScreenShot] (https://github.com/dudongge/DDGScreenShot) Welcome to view [DDGScreenShot] (https://github.com/dudongge/DDGScreenShot), of course, this is just a small part of the library's function Want to see more, can go to download, making if you have any help to you, I hope you will give me a star. The function code is as follows:Copy the code

DDGScreenShot all feature presentations

Specific code

1. Use UIBezierPath to draw pathsletImage = UIImage (named: imageName) / / open context UIGraphicsBeginImageContext ((image? .size)!) // Set a circular clipping arealetpath = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: (image? .size.width)! , height: (image? .size.height)!) ) // Set the path to the clipping area (anything outside the clipping area will be clipped automatically) path.addClip() .draw(at: cgpoint.zero) // Generate an image from the contextletNewImage = UIGraphicsGetImageFromCurrentImageContext () / / close context UIGraphicsEndImageContext newImage () is the picture we needCopy the code

In case you’re wondering about performance, ok, ok, let’s do it in a child thread, code like this

Dispatchqueue.global ().async{// Send the above codeletImage = UIImage (named: imageName) / / open context UIGraphicsBeginImageContext ((image? .size)!) // Set a circular clipping arealetpath = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: (image? .size.width)! , height: (image? .size.height)!) ) // Set the path to the clipping area (anything outside the clipping area will be clipped automatically) path.addClip() .draw(at: cgpoint.zero) // Generate an image from the contextletNewImage = UIGraphicsGetImageFromCurrentImageContext () / / close context UIGraphicsEndImageContext () Dispatchqueue.main. async(execute: {// Get newImage to the main thread update UI completed(newImage)})}Copy the code

This seems to have solved the problem perfectly, but what about fixed rounded corners with rounded corners

Public func tailoringImage(_ imageName: cornerRadius */ func tailoringImage(_ imageName: cornerRadius */ String,withRadius radius: CGFloat) -> UIImage? {let image = UIImage(named: imageName)
        if image == nil {
            returnUIImage ()} / / open context UIGraphicsBeginImageContext ((image? .size)!) // Set a circular clipping arealetpath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: (image? .size.width)! , height: (image? .size.height)!) , cornerRadius: radius) // Set the path to the clipping area (everything outside the clipping area is automatically clipped) path.addClip() // Draw the image into the context. .draw(at: cgpoint.zero) // Generate an image from the contextletNewImage = UIGraphicsGetImageFromCurrentImageContext () / / close context UIGraphicsEndImageContext ()return newImage
    }
Copy the code

It can also be done in child threads

/** ** Cut images with arbitrary corners in asynchronous drawing mode -imagename -- pass header name -cornerRadius -- Set the size of the corners -parameter completed: Public func async_tailoringImage(_ imageName: String,withRadius radius: CGFloat,completed:@escaping (UIImage?) -> ()) -> Void { DispatchQueue.global().async{letnewImage = self.tailoringImage(imageName, withRadius: radius) DispatchQueue.main.async(execute: {completed(newImage)})}} {completed(newImage)}} {completed(newImage)}}Copy the code

Cut corners and add borders

/** ** drawing method crop the image to a rounded corner and add a border - imageName - pass header name - borderWidth - border size - borderColor - borderColor */ public func tailoringImageLayer(_ image: UIImage,borderWidth width:CGFloat,borderColor color: UIColor ) -> UIImage? {//1. Start an image context and add the width and height of the original image to double the border width.letimageSize = CGSize(width: image.size.width + width * 2 , height: image.size.height + width * 2) UIGraphicsBeginImageContext(imageSize) //2. Fill a circular path. The circular path size is the same as the context size. // Draw the large circle into the context.letpath = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: imageSize.width, height: Imagesize.height)) // Color set color.set() // fill path.fill() //3. Add a small circle, small circle,x,y from the border width position, width and height of the original image. Set the circle as the clipping area.letclipPath = UIBezierPath(ovalIn: CGRect(x: width, y: width, width: image.size.width, height: Image.sie.height) // set the circle to clipping area. Image.draw (at: CGPoint(x: width, y: width)) //5. Generates an image from the contextletnewImage = UIGraphicsGetImageFromCurrentImageContext() //6. Close the context UIGraphicsEndImageContext ()returnNewImage} /** ** cut the image to round corners and add a border - imageName - pass header name - borderWidth - border size - borderColor - borderColor - parameter */ public func async_tailoringImageLayer(_ image: UIImage,borderWidth width:CGFloat,borderColor color: UIColor ,completed:@escaping (UIImage?) -> ()) -> Void { DispatchQueue.global().async{let newImage = self.tailoringImageLayer(image, borderWidth: width, borderColor: color)
            DispatchQueue.main.async(execute: {
                completed(newImage)
            })
        }
    }
Copy the code

On the picture

conclusion

That was the end of it, This code has been uploaded to githup [DDGScreenShot] (https://github.com/dudongge/DDGScreenShot) [link] (https://github.com/dudongge/DDGScreenShot), of course, this is only a fraction of the library functions Want to see more, can go to dead simple to download, if there is a help to you, I hope you feel free to give a star.Copy the code

Welcome to DDGScreenShot

The remaining functions are as follows

  1. DDGScreenShot – Complex screen screenshots (such as View ScrollView webView wkwebView)
  2. (2)DDGScreenShot- iOS image processing – multi-image Mosaic
  3. DDGScreenShot–iOS images cropped, cut corners, add borders, and you use a cornerRadius, and there are more advanced uses
  4. (4)DDGScreenShot – Picture eraser function
  5. (5)DDGScreenShot – Take any part of an image
  6. (6)DDGScreenShot – pictures with a variety of filters high screen operation