I was looking how to make UIImageView
circle. And all I can find is:
(Image has to be square)
image.layer.cornerRadius = image.frame.size.height/2
image.clipsToBounds = true
But I can’t find "image" and "layer" property in apple documentation. So I am not sure how to use it. Now I got such an error:
use of unresolved identifier ‘image’
Note: I have used image
higher in my code and it was no problem.
2
Answers
The
.layer.cornerRadius
part will be accessible only onUIImageView
instance that ismyImage
in your case.The
myImage.image
points toUIImage
that won’t give you access for.layer.cornerRadius
part.The
UIImage
is the image resource that you wish to see on screen while theUIImageView
is the one that’s presenting that resource on screen.Here’s what you can do –
You say you are trying to make a
UIImageView
into a circular shape. Then you show codeimage.layer.cornerRadius = image.frame.size.height/2
. If you are working with an image view, name itmyImageView
, orimageView
, notimage
. In the rest of my answer I refer to an image view namedmyImageView
.UIView
objects have lots of different properties. Open the Xcode documentation and look up theUIView
class reference for more information about the many properties ofUIView
objects.UIKit is an object-oriented framework, so classes inherit from other classes. A
UIImageView
is a subclass ofUIView
. It has all the properties of it’s parent class, plus properties unique to an image view.A
UIImageView
is a view that is meant to display an image. In addition toUIView
properties, it has a propertyimage
which holds the image the image view is supposed to display.If you declare an image view:
Then you might install an image into that image view using code like
That says "Evaluate the stuff on the right side of the equals sign. Assign the result of that to the thing on the left side of the equals sign."
UIImage(named:)
tries to load aUIImage
with a specific name from your app’s "bundle" (The folder that contains all the stuff in your app.) If it succeeds, it returns an image. If not, it returns nil.myImageView.image
refers to theimage
property of theUIImageView
myImageView
. So, that code says "Try to load an image named ‘icon’ from the app’s directory, and install it into the image viewmyImageView
.")Layers:
All
UIView
objects have a "backing layer". Under the covers, UIKit uses the CoreAnimation framework to draw the view to the screen, and the main object in Core Animation that draws to the screen is aCALayer
.When you say
myImageView.layer.cornerRadius = myImageView.frame.size.height/2
, you are saying "calculate 1/2 of the height of my image view’s frame (The rectangle that defines it’s place on the screen) and use that to set the corner radius of the view’s layer."What that does, for square views (in combination with the next line of your code,
myImageView.clipsToBounds = true
) is to make the view’s layer (The thing that’s drawn to the screen) into a circle shape. The corners of the square view are clipped away, and all the user can see is the circular part in the middle.