我在ViewController tableView中有标识符“Cell”。这是我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
var url = NSURL(string: postCover[indexPath.row])
let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
cell.imageView?.contentMode = .ScaleAspectFit
cell.imageView?.clipsToBounds = true
cell.imageView?.image = UIImage(data: data!)
cell.textLabel?.text = postTitle[indexPath.row]
cell.imageView?.layer.cornerRadius = 20
cell.imageView?.layer.masksToBounds = true;
return cell
}
所以,cornerRadius
有效,但其中一个问题是单元格(cell.imageView?.image
)高度中的图像等于行高,我既不能改变高度,也不能改变这个单元格图像的宽度
当我尝试
时cell.imageView?.frame.size.width = 100
没有什么变化。谁知道,问题是什么?
由于我无法控制图像的高度和宽度,因此我得到带有边框半径的结果矩形,但我想要一个正方形(w:100,h:100)。
答案 0 :(得分:4)
您可以使用此扩展程序裁剪图像的最长边并返回平方UIImage:
extension UIImage {
var squared: 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
UIGraphicsBeginImageContext(imageView.bounds.size)
imageView.layer.renderInContext(UIGraphicsGetCurrentContext())
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result
}
var circle: 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.masksToBounds = true
UIGraphicsBeginImageContext(imageView.bounds.size)
imageView.layer.renderInContext(UIGraphicsGetCurrentContext())
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result
}
func resizeToWidth(width:Int)-> UIImage {
let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: CGFloat(width), height: CGFloat(ceil(CGFloat(width)/size.width * size.height)))))
imageView.contentMode = UIViewContentMode.ScaleAspectFit
imageView.image = self
UIGraphicsBeginImageContext(imageView.bounds.size)
imageView.layer.renderInContext(UIGraphicsGetCurrentContext())
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result
}
}
答案 1 :(得分:0)
您无法单独更改框架宽度或高度。你必须设置完整的框架。在Objective C中它就像是
cell.imageView.frame = CGRectMake(cell.imageView.frame.origin.x, cell.imageView.frame.origin.y, 100, 100);
答案 2 :(得分:0)
您无法更改任何视图的frame属性的属性。你必须提供一个全新的框架。
cell.imageView?.frame.size.width = 100 //wont work
尝试这样做 -
let rect = cell.imageView?.frame;
rect.size.width = 100;
cell.imageView?.frame = rect; //this will work