Crop image to be circular swift 3

2019-08-02 18:28发布

问题:

So I have this issue I have been trying to solve for the past few days however, cannot find a solution to the problem. My issue is very simple I am trying to crop a image so that it becomes a circle. From research done, I have found a few methods that can be used to achieve this and the main one which seemed to pop up a lot is to simply crop the UIImageView so that it becomes a circular frame. The code which I used to do this can be seen below.

imageView.layer.masksToBounds = false
imageView.layer.cornerRadius = imageView.frame.height/2
imageView.clipsToBounds = true

The only problem is that this method only crops the frame of the image view and not the image itself. So if I was to save the image to the users camera roll for instance, it would still appear as a rectangle and not a cropped circle. I am a bit lost with how I can actually achieve this as I am relatively new to Xcode and was hoping that someone might be able to provide a method as to how I can crop the image itself not just the frame. Any help on the topic will be greatly appreciated. Thank you.

回答1:

Lets get on **Graphics Context **.

func makeRoundImg(img: UIImageView) -> UIImage {
    let imgLayer = CALayer()
    imgLayer.frame = img.bounds
    imgLayer.contents = img.image?.cgImage;
    imgLayer.masksToBounds = true;

    imgLayer.cornerRadius = 28 //img.frame.size.width/2

    UIGraphicsBeginImageContext(img.bounds.size)
    imgLayer.render(in: UIGraphicsGetCurrentContext()!)
    let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return roundedImage!;
}


回答2:

Try this code for swift 3, first you need to kept the imageView content mode as Aspect Fit.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var imgView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    let profilePicture = UIImage(named: "yourImageName")
    self.imgView.image = profilePicture.circleMasked
   }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



}

  extension UIImage {
  var isPortrait:  Bool    { return size.height > size.width }
  var isLandscape: Bool    { return size.width > size.height }
  var breadth:     CGFloat { return min(size.width, size.height) }
  var breadthSize: CGSize  { return CGSize(width: breadth, height: breadth) }
  var breadthRect: CGRect  { return CGRect(origin: .zero, size: breadthSize) }
  var circleMasked: UIImage? {
      UIGraphicsBeginImageContextWithOptions(breadthSize, false, scale)
      defer { UIGraphicsEndImageContext() }
      guard let cgImage = cgImage?.cropping(to: CGRect(origin: CGPoint(x: isLandscape ? floor((size.width - size.height) / 2) : 0, y: isPortrait  ? floor((size.height - size.width) / 2) : 0), size: breadthSize)) else { return nil }
      UIBezierPath(ovalIn: breadthRect).addClip()
      UIImage(cgImage: cgImage, scale: 1, orientation: imageOrientation).draw(in: breadthRect)
      return UIGraphicsGetImageFromCurrentImageContext()
  }
}


回答3:

Try following

imageView.layer.masksToBounds = false
imageView.layer.cornerRadius = imageView.frame.height/2
imageView.layer.borderWidth = 1
imageVoew.layer.borderColor = UIColor.clear.cgColor
imageView.clipsToBounds = true