26

I have tried Googling my problem but cannot seem to find anything anywhere! Basically I have a UIView which retrieves a Users Facebook Photo once they log into my app. At the moment it is a square, but I cannot figure out at all how to turn the UIView into a circle... Any help would be greatly appreciated, I am still learning! -Alex

This Just connects it to the UIView..

@IBOutlet var profilePictureView : FBProfilePictureView!

This sets the View as the Users FB Profile Photo

profilePictureView.profileID = user.objectID

And in app delegate.swift

FBProfilePictureView.self

Thats all the code, I set the UIView's class to be FBProfilePictureView.

alexgarbz
  • 289
  • 1
  • 3
  • 8

2 Answers2

72

You can try this

 profilePictureView.layer.cornerRadius = profilePictureView.frame.size.width/2
 profilePictureView.clipsToBounds = true

 profilePictureView.layer.borderColor = UIColor.whiteColor().CGColor
 profilePictureView.layer.borderWidth = 5.0
rakeshbs
  • 24,392
  • 7
  • 73
  • 63
  • The only problem is, is that it isn't an image view, rather just a normal UIView. I'm using the Facebook SDK to retrieve a user's photo and place it in the UiView – alexgarbz Feb 09 '15 at 13:27
  • how do you place it in the uiview? – rakeshbs Feb 09 '15 at 13:27
  • Just using the FBProfilePictureView Class – alexgarbz Feb 09 '15 at 13:30
  • set the mask to `fbpictureview.layer.mask = clip layer` – rakeshbs Feb 09 '15 at 13:31
  • It's just bringing up errors: "Expected Declaration" and "does not have a member named frame". – alexgarbz Feb 09 '15 at 13:39
  • I am using storyboards and as of yet I have no code specifically for that UIView... I have my code for the rest of the View if you want, but thats just a populated tableview and stretchy header. – alexgarbz Feb 09 '15 at 13:42
  • i just want to see the code where you add the FBProfilePictureView to the uiview – rakeshbs Feb 09 '15 at 13:43
  • what happens if you set my code after `profilePictureView.profileID = user.objectID` – rakeshbs Feb 09 '15 at 13:57
  • I have defined my uiImageView (100x100 points) in the storyboard and somehow the cornerRadius value I'm using (imageview.frame.size.width/2) doesn't work for all the different resolution. For iPhone 4S it makes it look like a diamond. What am I doing wrong? – Claus Jun 05 '15 at 11:19
  • Is anyone facing this issue in Xcode 8? Mine was working in Xcode 7 but after upgrading to Xcode 8, without touching any codes, the circle is not drawn at all. – jo3birdtalk Sep 25 '16 at 09:08
1

Try defining a UIBezier path as a circle and then add it as a clip to your view:

let circle = UIBezierPath(arcCenter: (CGPoint), radius: (CGFloat), startAngle: 0, endAngle: 2*M_PI)

double check, but I think arcCenter is in your superview's co-ordinate system.

To add it as a clipping path, I think it's just: (You'll have to check the docs for details):

circle.addClip()
Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • Could you possibly help me out with that? Or just explain it a little.. I need the help to learn but i can't find any info anywhere! – alexgarbz Feb 09 '15 at 12:58
  • Thanks for this, but I'm really still not sure where or how to implement this.. – alexgarbz Feb 09 '15 at 13:06
  • Alex - it's not something I've used myself, but this is where I'd start. I think you'll also need your own UIView (i,e, a sub-class of UIView) and the drawing and clipping will be implemented in your UIView's drawRect function. – Steve Ives Feb 09 '15 at 13:17
  • Thanks Steve, I might just give up on this bit until i learn a bit more. Confusing the hell out of me :P – alexgarbz Feb 09 '15 at 13:18