How Use UIImageView In UITableView Cell in Swift Ios/Iphone
=> Here way to use UIImageview with Class :
class ViewController: UIViewController {
var bgImage: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
var image: UIImage = UIImage(named: "afternoon")!
bgImage = UIImageView(image: image)
bgImage!.frame = CGRectMake(0,0,100,200)
self.view.addSubview(bgImage!)
}
}
=> Another way to use UIImageview:
@IBOutlet weak var bgImage: UIImageView!
var image : UIImage = UIImage(named:"afternoon")!
bgImage = UIImageView(image: image)
bgImage.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
view.addSubview(bgImage)
=> Another way to use UIImageview:
var imageViewObject :UIImageView
imageViewObject = UIImageView(frame:CGRectMake(0, 0, 600, 600))
imageViewObject.image = UIImage(named:"afternoon")
self.view.addSubview(imageViewObject)
self.view.sendSubviewToBack(imageViewObject)
=> here Resizing images:
func resizeImage(image:UIImage, toTheSize size:CGSize)->UIImage{
var scale = CGFloat(max(size.width/image.size.width,
size.height/image.size.height))
var width:CGFloat = image.size.width * scale
var height:CGFloat = image.size.height * scale;
var rr:CGRect = CGRectMake( 0, 0, width, height);
UIGraphicsBeginImageContextWithOptions(size, false, 0);
image.drawInRect(rr)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return newImage
}
=> Now Assign Image to Cell Imageview:-
let imageName = "11749-simple.jpg"
let image = UIImage(named: imageName)
let newImage = resizeImage(image!, toTheSize: CGSizeMake(70, 70))
var cellImageLayer: CALayer? = cell.cellImage.layer
cellImageLayer!.cornerRadius = cellImageLayer!.frame.size.width / 2
cellImageLayer!.masksToBounds = true
cell.cellImage.image = newImage
=> Or you can try this extension:
extension UIImage {
var circleMask: UIImage {
let square = size.width < size.height ? CGSize(width: size.width, height: size.width) : CGSize(width: size.height, height: size.height)
let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: square))
imageView.contentMode = UIViewContentMode.ScaleAspectFill
imageView.image = self
imageView.layer.cornerRadius = square.width/2
imageView.layer.borderColor = UIColor.whiteColor().CGColor
imageView.layer.borderWidth = 5
imageView.layer.masksToBounds = true
UIGraphicsBeginImageContext(imageView.bounds.size)
imageView.layer.renderInContext(UIGraphicsGetCurrentContext())
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result
}
class CellComment: UITableViewCell
{
@IBOutlet weak var imageV_profile: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
imageV_profile.layer.cornerRadius = 25
imageV_profile.layer.masksToBounds = true
}
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell:CellPost! = tableView.dequeueReusableCellWithIdentifier("CellPost") as? CellPost
if (cell == nil)
{
let nib:Array = NSBundle.mainBundle().loadNibNamed("CellPost", owner: self, options: nil)
cell = nib[0] as? CellPost
}
cell.imageV_profile.image = UIImage(named: "Your image name")
return cell
}
class ViewController: UIViewController {
var bgImage: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
var image: UIImage = UIImage(named: "afternoon")!
bgImage = UIImageView(image: image)
bgImage!.frame = CGRectMake(0,0,100,200)
self.view.addSubview(bgImage!)
}
}
=> Another way to use UIImageview:
@IBOutlet weak var bgImage: UIImageView!
var image : UIImage = UIImage(named:"afternoon")!
bgImage = UIImageView(image: image)
bgImage.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
view.addSubview(bgImage)
=> Another way to use UIImageview:
var imageViewObject :UIImageView
imageViewObject = UIImageView(frame:CGRectMake(0, 0, 600, 600))
imageViewObject.image = UIImage(named:"afternoon")
self.view.addSubview(imageViewObject)
self.view.sendSubviewToBack(imageViewObject)
=> here Resizing images:
func resizeImage(image:UIImage, toTheSize size:CGSize)->UIImage{
var scale = CGFloat(max(size.width/image.size.width,
size.height/image.size.height))
var width:CGFloat = image.size.width * scale
var height:CGFloat = image.size.height * scale;
var rr:CGRect = CGRectMake( 0, 0, width, height);
UIGraphicsBeginImageContextWithOptions(size, false, 0);
image.drawInRect(rr)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return newImage
}
=> Now Assign Image to Cell Imageview:-
let imageName = "11749-simple.jpg"
let image = UIImage(named: imageName)
let newImage = resizeImage(image!, toTheSize: CGSizeMake(70, 70))
var cellImageLayer: CALayer? = cell.cellImage.layer
cellImageLayer!.cornerRadius = cellImageLayer!.frame.size.width / 2
cellImageLayer!.masksToBounds = true
cell.cellImage.image = newImage
=> Or you can try this extension:
extension UIImage {
var circleMask: UIImage {
let square = size.width < size.height ? CGSize(width: size.width, height: size.width) : CGSize(width: size.height, height: size.height)
let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: square))
imageView.contentMode = UIViewContentMode.ScaleAspectFill
imageView.image = self
imageView.layer.cornerRadius = square.width/2
imageView.layer.borderColor = UIColor.whiteColor().CGColor
imageView.layer.borderWidth = 5
imageView.layer.masksToBounds = true
UIGraphicsBeginImageContext(imageView.bounds.size)
imageView.layer.renderInContext(UIGraphicsGetCurrentContext())
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result
}
class CellComment: UITableViewCell
{
@IBOutlet weak var imageV_profile: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
imageV_profile.layer.cornerRadius = 25
imageV_profile.layer.masksToBounds = true
}
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell:CellPost! = tableView.dequeueReusableCellWithIdentifier("CellPost") as? CellPost
if (cell == nil)
{
let nib:Array = NSBundle.mainBundle().loadNibNamed("CellPost", owner: self, options: nil)
cell = nib[0] as? CellPost
}
cell.imageV_profile.image = UIImage(named: "Your image name")
return cell
}