Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Stephen Wall
PLUS
Stephen Wall
Courses Plus Student 27,294 Points

iOS TableViews and Rounded Images (contact photos/ avatars)

Hey all. I just completed the iOS intermediate course for tableviews. I found a way to make the contact photos round within the tableview and thought I would share so you could give it a try. It looks great! Just add this extension in your project. Since it is public, you can access this method in other files/ classes/ TableView Controllers. I used it to make all the contact photos rounded in the master view, and then the profile photo as well in the detail view.

// Making the image Views Circular. 
public extension UIImage {
    var circle: UIImage {
        let square = size.width < size.height ? CGSize(width: size.width, height: size.width) : CGSize(width: size.height, height: size.height)
        let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: square))
        imageView.contentMode = UIView.ContentMode.scaleAspectFill
        imageView.image = self
        imageView.layer.borderWidth = 3.0
        imageView.layer.borderColor = UIColor.black.cgColor
        imageView.layer.cornerRadius = square.width / 2
        imageView.layer.masksToBounds = true
        UIGraphicsBeginImageContext(imageView.bounds.size)
        imageView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result!
    }
}

Once you have this in, you can call it on an instance of UIImage that you'd like to display within a UIImageView:

contactImageView.image = contact.image?.circle