I need to display a profile pic of every user corresponding to his name in a UITableView. Until the image is downloaded I need to show an image with his name's first alphabet like in the GMail app. How to do this programmatically in Swift for iOS?
"NO NEED ANY FRAMEWORK"
"Its Work Wonderfully"
@IBOutlet weak var IBtxtFieldName:UITextField!
@IBOutlet weak var IBtxtFieldSurname:UITextField!
@IBOutlet weak var IBImgViewUserProfile:UIImageView!
@IBAction func IBbtnShowTapped(sender: UIButton)
{
let lblNameInitialize = UILabel()
lblNameInitialize.frame.size = CGSize(width: 100.0, height: 100.0)
lblNameInitialize.textColor = UIColor.whiteColor()
lblNameInitialize.text = String(IBtxtFieldName.text!.characters.first!) + String(IBtxtFieldSurname.text!.characters.first!)
lblNameInitialize.textAlignment = NSTextAlignment.Center
lblNameInitialize.backgroundColor = UIColor.blackColor()
lblNameInitialize.layer.cornerRadius = 50.0
UIGraphicsBeginImageContext(lblNameInitialize.frame.size)
lblNameInitialize.layer.renderInContext(UIGraphicsGetCurrentContext()!)
IBImgViewUserProfile.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
"SWIFT 3.0"
@IBAction func IBbtnShowTapped(sender: UIButton)
{
let lblNameInitialize = UILabel()
lblNameInitialize.frame.size = CGSize(width: 100.0, height: 100.0)
lblNameInitialize.textColor = UIColor.white
lblNameInitialize.text = String(IBtxtFieldName.text!.characters.first!) + String(IBtxtFieldSurname.text!.characters.first!)
lblNameInitialize.textAlignment = NSTextAlignment.center
lblNameInitialize.backgroundColor = UIColor.black
lblNameInitialize.layer.cornerRadius = 50.0
UIGraphicsBeginImageContext(lblNameInitialize.frame.size)
lblNameInitialize.layer.render(in: UIGraphicsGetCurrentContext()!)
IBImgViewUserProfile.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
This is perfect for the UIImageView: https://github.com/bachonk/UIImageView-Letters. Basically, it creates a UIImage with, at the center, the initial letter of the first and last word of the input. The background color can be random or assigned.
Here's an example of what this category can do:
[EDIT]
You may also want to check this out: https://github.com/bofiaza/IPImage
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