skip to Main Content

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.

this is my code

2

Answers


  1. The .layer.cornerRadius part will be accessible only on UIImageView instance that is myImage in your case.

    The myImage.image points to UIImage that won’t give you access for .layer.cornerRadius part.

    The UIImage is the image resource that you wish to see on screen while the UIImageView is the one that’s presenting that resource on screen.

    Here’s what you can do –

    myImage.layer.cornerRadius = myImage.frame.size.height/2
    myImage.clipsToBounds = true
    
    Login or Signup to reply.
  2. You say you are trying to make a UIImageView into a circular shape. Then you show code image.layer.cornerRadius = image.frame.size.height/2. If you are working with an image view, name it myImageView, or imageView, not image. In the rest of my answer I refer to an image view named myImageView.

    UIView objects have lots of different properties. Open the Xcode documentation and look up the UIView class reference for more information about the many properties of UIView objects.

    UIKit is an object-oriented framework, so classes inherit from other classes. A UIImageView is a subclass of UIView. 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 to UIView properties, it has a property image which holds the image the image view is supposed to display.

    If you declare an image view:

    @IBOutlet myImageView: UIImageView
    

    Then you might install an image into that image view using code like

    myImageView.image = UIImage(named: "icon")
    

    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 a UIImage 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 the image property of the UIImageView myImageView. So, that code says "Try to load an image named ‘icon’ from the app’s directory, and install it into the image view myImageView.")


    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 a CALayer.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search