简体   繁体   中英

Circle image crop when using UIImagePickerController Swift

I need to crop images circle when importing from photo library, just like in the stock contacts app. I found a few solutions but all of them were in Objective-C. I found them hard to translate. Does anyone know a useful swift library or so?

this works for me

    profileImage.image = UIImageVar
    profileImage.layer.borderWidth = 1
    profileImage.layer.borderColor = UIColor.blackColor().CGColor
    profileImage.layer.cornerRadius = profileImage.frame.height/2
    profileImage.clipsToBounds = true

If you're seeking something like Core Graphics, you can take the following for references.

let ctx = UIGraphicsGetCurrentContext()
let image = UIImage(named: "YourImageName")!

// Oval Drawing
let width = image.size.width
let height = image.size.height
let ovalPath = UIBezierPath(ovalInRect: CGRectMake(0, 0, width, height))
CGContextSaveGState(ctx)
ovalPath.addClip()
image.drawInRect(CGRectMake(0, 0, width, height))
CGContextRestoreGState(ctx)

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