in storyboard (xcode 6) i want a circular user image profile take from Facebook.
So i have make this interface in storyboard, using auto layout:
Then, using Facebook iOS sdk i take the user profile (using swift):
var facebookProfileUrl = "http://graph.facebook.com/\(userId!)/picture?type=normal";
In storyboard i have set the image to "Scale to fit" mode. To make the image view circular i use the following code:
self.facebookProfileImage.layer.cornerRadius = self.facebookProfileImage.frame.size.width / 2;
self.facebookProfileImage.clipsToBounds = true;
When i run the code, anyway the image doesn't look circular:
I suppose the problem is auto layout but i'm not sure. How can i make the image perfectly circular??
In storyboards, you add views by dragging them onto the view controller scene. For example, the following figure shows a view controller with an image view and button on an iPhone.
ImageView can be defined as an object that can display the images on the interface of the iOS applications. It is the instance of the UIImageView class, which inherits UIView.
Two steps:
Why? The UIImageView is getting stretched because Auto Layout needs to account for the leading and trailing constraints you set on the UIImageView. To prove my point, set the priority of the leading and trailing constraints to something less than the priority of the height and width constraints. You should see a rounded image like you expect, but it may not be centered.
More steps:
set corner radius to half of either its height or width
yourImageViewOutlet.layer.cornerRadius = yourImageViewOutlet.frame.size.width / 2.0
I have made the same thing a little time ago and this worked for me
self.imageView.image = [ImageHelper getImage]; //retrieve image
self.imageView.layer.cornerRadius = self.imageView.frame.size.height / 2;
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.borderWidth = 0;
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With