Swift中的UITableViewCell中没发直接给imageView赋值,
所以想要自定义头像的话,只能通过给imageView.image去赋值:
cell.imageView?.image=UIImage(named:”hdImg_wutao_48x48.jpg”) |
但是现在需要的是:
需要画一个图形,然后赋值到imageView中。
所以需要:
swift画一个image
[解决过程]
1.搜:
swift draw image
swift draw image not imageview
参考:
最后用:
func drawRectangleImage(size:CGSize, color:UIColor) -> UIImage { let opaque = false let scale:CGFloat = 0 UIGraphicsBeginImageContextWithOptions(size, opaque, scale) let context = UIGraphicsGetCurrentContext() //CGContextSetLineWidth(context, 4.0) //CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor) let rectangle = CGRectMake(0,0,size.width,size.height) CGContextAddRect(context, rectangle) //CGContextStrokePath(context) CGContextSetFillColorWithColor(context, color.CGColor) CGContextFillRect(context, rectangle) // Drawing complete, retrieve the finished image and cleanup let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } func drawConversationHeaderImage(size: CGSize) -> UIImage { let headerRectangleImage = drawRectangleImage(size, color:UIColor.purpleColor()) print("headerRectangleImage.size=\(headerRectangleImage.size)") return headerRectangleImage } //cell.imageView?.image = UIImage(named:"hdImg_xxx_48x48.jpg") let drawedCustomHeaderImage = drawConversationHeaderImage(CGSizeMake(48, 48)) cell.imageView?.image = drawedCustomHeaderImage
画出了矩形的image:
转载请注明:在路上 » [已解决]swift画一个image而不是ImageView