Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 667 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to set image in circle in swift

#1
How can i make i circle picture with swift ?

My ViewController :

import UIKit
import Foundation

class FriendsViewController : UIViewController{

@IBOutlet weak var profilPicture: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
profilPicture = UIImageView(frame: CGRectMake(0, 0, 100, 100))
}
}

My `profilPicture = UIImageView(frame: CGRectMake(0, 0, 100, 100))` do nothing ..

Exemple:

[To see links please register here]

Reply

#2
extension UIImageView{

// Round Image
func roundCorner() {
self.layer.masksToBounds = true
self.layer.borderWidth = 1.0
self.layer.borderColor = UIColor.clear.cgColor
self.layer.cornerRadius = self.frame.size.width/2
}
}
Reply

#3
import UIKit

class ViewController: UIViewController {
@IBOutlet weak var image: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

image.layer.borderWidth = 1
image.layer.masksToBounds = false
image.layer.borderColor = UIColor.black.cgColor
image.layer.cornerRadius = image.frame.height/2
image.clipsToBounds = true
}

If you want it on an extension


import UIKit

extension UIImageView {

func makeRounded() {

layer.borderWidth = 1
layer.masksToBounds = false
layer.borderColor = UIColor.black.cgColor
layer.cornerRadius = self.frame.height / 2
clipsToBounds = true
}
}

That is all you need....
Reply

#4
For Rounded Image, Only following code would be enough in Extension:

self.layoutIfNeeded() // in case of **autolayout**(mentioned by Tom Spee in the comment).

And also make sure the property of UIImageView **contentMode** should be set correctly to get the result.
In my case it is:

imageView.contentMode = .scaleAspectFit


extension UIImageView {
func setRounded() {
self.layoutIfNeeded()
self.layer.cornerRadius = self.frame.height / 2
self.clipsToBounds = true
}
}

Reply

#5
imageView.layer.cornerRadius = imageView.frame.height/2
imageView.clipToBounds = true

Reply

#6
struct CircleImage: View {
var image: Image

var body: some View {
image
.clipShape(Circle())
}
}

This is correct for `SwiftUI`
Reply

#7
// code to make the image round


import UIKit

extension UIImageView {
public func maskCircle(anyImage: UIImage) {


self.contentMode = UIViewContentMode.ScaleAspectFill
self.layer.cornerRadius = self.frame.height / 2
self.layer.masksToBounds = false
self.clipsToBounds = true




// make square(* must to make circle),
// resize(reduce the kilobyte) and
// fix rotation.
// self.image = prepareImage(anyImage)
}
}



// to call the function from the view controller

self.imgCircleSmallImage.maskCircle(imgCircleSmallImage.image!)
Reply

#8
This way is the least expensive way and always keeps your image view rounded:

class RoundedImageView: UIImageView {

override init(frame: CGRect) {
super.init(frame: frame)

clipsToBounds = true
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

clipsToBounds = true
}

override func layoutSubviews() {
super.layoutSubviews()

assert(bounds.height == bounds.width, "The aspect ratio isn't 1/1. You can never round this image view!")

layer.cornerRadius = bounds.height / 2
}
}

The other answers are telling you to make views rounded based on frame calculations set in a `UIViewController`s `viewDidLoad()` method. **This isn't correct**, since it isn't sure what the final frame will be.
Reply

#9
Based in the answer of @DanielQ

**Swift 4 and Swift 3**

import UIKit

extension UIImageView {

func setRounded() {
self.layer.cornerRadius = (self.frame.width / 2) //instead of let radius = CGRectGetWidth(self.frame) / 2
self.layer.masksToBounds = true
}
}

You can use it in any `ViewController` with:

imageView.setRounded()


Reply

#10
For **Swift 4**:

import UIKit

extension UIImageView {

func makeRounded() {
let radius = self.frame.width/2.0
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
}
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through