I'm trying to make a circular image in Swift, I've searched around and watched a couple YouTube videos. The solutions proposed are extremely easy, but when I used them I get an image formed as an eye instead of a circle, below is my view controller and a picture of the UI
var experimentIdentifier: String = ""
#IBOutlet weak var foregroundImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
foregroundImage.layer.cornerRadius = (foregroundImage.frame.size.width) / 2
foregroundImage.layer.masksToBounds = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
It's because your image is not a square.
See this problem in action:
Set the image to a width : height = 2 : 1 rectangle in storyboard
then run, the image will look like
but if the image is a square, like this in storyboard
click run, it'll be like
Enjoy coding!
Try This ,
let profileImageView = UIImageView()
profileImageView.frame = CGRectMake(150, 150, 60, 60)
profileImageView.layer.borderWidth = 1
profileImageView.layer.borderColor = UIColor.grayColor().CGColor
profileImageView.backgroundColor = UIColor.yellowColor()
profileImageView.layer.cornerRadius = 60/2
let image = UIImage(named: "yourImageName")
profileImageView.layer.cornerRadius = profileImageView.frame.size.height/2
profileImageView.clipsToBounds = true
Your image is not a square i guess. To make it square you have to make sure that height and width of the image are equals and then also add these two properties as well
foregroundImage.layer.cornerRadius = 0.5 * your image height
foregroundImage.layer.masksToBounds = true
foregroundImage.clipsToBounds = true
I hope this will help
make sure the image is a square then add this code
foregroundImage.layer.cornerRadius = side/2
foregroundImage.layer.masksToBounds = true
Related
So I'm following the the normal approach in Turing an image into circle :
image.layer.cornerRadius = image.frame.size.width/2.0f;
image.layer.borderColor = [UIColor blackColor].CGColor;
image.layer.borderWidth = 2;
image.clipsToBounds = YES ;
However when I used it in cells, first time it shows in Simi-perfect circle, but when I scroll to show new cells , all will be in perfect circle shape.
so my question is : why the first visible cells appear in a Simi-cirlce shape ?
This is how it looks like first time , but if I refresh or reload the page, everything is fine
Try to use this way
like in custom cell class
class ProfilePicCell: UITableViewCell {
#IBOutlet weak var image: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
image.layer.cornerRadius = image.frame.width / 2
image.layer.masksToBounds = true
}
}
in Objective C add this method in custom cell class
- (void)awakeFromNib
{
super.awakeFromNib()
image.layer.cornerRadius = image.frame.width / 2
}
Override layoutSubviews in the cell and after calling super.layoutSubviews update the image.layer.cornerRadius there. Then when the layout of the cell is updated after the cell gets visible, the corner radius will be updated accordingly.
So in swift, in your cell implementantion you would do something like:
override func layoutSubviews() {
super.layoutSubviews()
// now the frames were recalculated, and we can update cornerRadius
image.layer.cornerRadius = image.bounds.size.width / 2.0
}
I have tried a couple different codes but couldn't make the logo show
import UIKit
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image: logo)
self.navigationItem.titleView = imageView
navigationItem.titleView?.sizeToFit()
}
}
I even tried to include IB in the class itself didn't work either, it doesn't seem to work that way
#IBOutlet weak var navBar: UINavigationItem!
PS. My logo is a 200x40px png and its named logo.png in the assets.
My storyboard
http://i68.tinypic.com/b68t8o.png
Any help is appreciated
Edit: I solved my problem by putting an image view there instead of this whole navigation item story. Thanks for your suggestions though.
try this
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image: logo)
imageView.contentMode = .ScaleAspectFit // set imageview's content mode
self.navigationItem.titleView = imageView
Set your imageview's content mode like,
imageView.contentMode = .ScaleAspectFit
This code looks good, i think there is error in name of UIImage, did you debug and check that let logo is not nil? try to remove ".jpg"
Use extension:
extension UIViewController {
func setNavigationBarLogo() {
let logo = UIImage(named: "YourLogo.png")
let imageView = UIImageView(image: logo)
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView
}
}
Usage in UIViewController:
override func viewDidLoad() {
super.viewDidLoad()
self.setNavigationBarLogo()
}
I feel like im overlooking something:
my image still shows up like a diamond shape. Im trying to get it to be circular.
class MenuViewController: UIViewController {
#IBOutlet var profileImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
profileImageView.layer.cornerRadius = profileImageView.frame.size.width / 2;
profileImageView.clipsToBounds = true;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
It is possible that the profileImageView is changing its size after the viewDidLoad and ending up smaller than it was when you set the corner radius. You can test this theory by moving your corner radius code to viewDidAppear.
It seems like you might need to change clipsToBounds = true to layer.masksToBounds = true, as the masking needs to happen on the layer, not on the image view itself. I have that same effect in my app, here's the code:
imageView.layer.cornerRadius = imageView.frame.size.width / 2.0
imageView.layer.borderColor = UIColor.whiteColor().CGColor
imageView.layer.borderWidth = 2.0
imageView.layer.masksToBounds = true
Hope that helps.
Hi I am making an app with a background that leaves a big blank white space at the top of the screen when I use the first code shown below and as soon as I add self.view.addSubview(image view) at the end of this code like the second piece of code it covers all my labels and buttons, how can I have it so that it takes up the whole screen like in the second part but doesn't cover up the buttons like in the first piece of code?
thanks !
first code
override func viewDidLoad() {
super.viewDidLoad()
let yourImage = UIImage(named: "fond320x480.png")
let imageview = UIImageView(image: yourImage)
self.view.addSubview(imageview)
self.view.sendSubviewToBack(imageview)
imageview.contentMode = UIViewContentMode.ScaleToFill
imageview.contentMode = UIViewContentMode.ScaleAspectFill
imageview.frame = self.view.bounds
second code:
override func viewDidLoad() {
super.viewDidLoad()
let yourImage = UIImage(named: "fond320x480.png")
let imageview = UIImageView(image: yourImage)
self.view.addSubview(imageview)
self.view.sendSubviewToBack(imageview)
imageview.contentMode = UIViewContentMode.ScaleToFill
imageview.contentMode = UIViewContentMode.ScaleAspectFill
imageview.frame = self.view.bounds
self.view.addSubview(imageview)
try to send it to the back using this method
insertSubview:yourSubView atIndex:0
and give it the index by zero.
or use this one
-insertSubview:aboveSubview:
the other solution is to add all your view in one view and then add the image and use method -belowSubView- to add the image under the view that has all your views
hope this answer can help good luck
My goal is to overlay two images (the first a photo from camera roll, the second a PNG of a cartoon ghost).
I've gotten far enough that I'm passing in a camera roll image and a selected ghost image to a view controller. But where I'm stuck is how to layer these images in a useable way.
I can flow in the original photo a couple of ways (either by starting with an image view or by programmatically creating one in a view), and I can add the ghost on top of it (I've mostly been doing this programmatically).
But I can only control the ghost's size and placement manually. I'd like it to come in centered on the original image and match either its height or width, depending on which is smaller as it can be horizontal or vertical.
After an evening of searching, I've come up blank. But surely there's got to be a way to grab the image view's coordinates and make that calculation, right?
Here's what I've got:
import UIKit
class TwoLayerViewController: UIViewController {
#IBOutlet weak var bottomLayerImage: UIImageView!
var originalPhoto: UIImage?
var chosenGhostPhoto: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
bottomLayerImage.image = originalPhoto
bottomLayerImage.contentMode = UIViewContentMode.ScaleAspectFit
var ghostView = UIImageView(frame:CGRectMake(bottomLayerImage.frame.origin.x, bottomLayerImage.frame.origin.y, 100, 100))
ghostView.image = chosenGhostPhoto
ghostView.backgroundColor = UIColor.grayColor()
bottomLayerImage.addSubview(ghostView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Try putting the top image not in an ImageView, but in a sublayer over the other one. Like this:
class YourImageView: UIImageView{
let containerLayer = CALayer()
func drawImageOnTop(img: UIImage){
let piclayer = CALayer()
let sz = originalPhoto?.size ?? CGSize(width: 0, height: 0)
piclayer.frame = CGRect(origin: self.layer.contentsRect.origin, size: sz)
piclayer.position = CGPoint(x: sz.width/2, y: sz.height/2)
piclayer.contentsGravity = .resizeAspect
piclayer.contents = img.cgImage
containerLayer.addSublayer(piclayer)
}