我正在开发一个社交网站,我有一个带有多个单元格(行)的 TableView,在每个单元格中我都有一个“评论”和“喜欢”Imageview,所以每当更新来自服务器时,我必须增加“评论”的数量”和“喜欢”通过徽章。那么如何在 Swift 的 TableViewCell 中的 UIImageView 上添加徽章
问问题
4600 次
3 回答
5
您可以通过这种方式在 UIImageView 上添加徽章
func drawImageView(mainImage: UIImage, withBadge badge: UIImage) -> UIImage
{
UIGraphicsBeginImageContextWithOptions(mainImage.size, false, 0.0)
mainImage.drawInRect(CGRectMake(0, 0, mainImage.size.width, mainImage.size.height))
badge.drawInRect(CGRectMake(mainImage.size.width - badge.size.width, 0, badge.size.width, badge.size.height))
let resultImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resultImage
}
然后使用这个功能
let mainImage = UIImage(named: "main_image_name_here")
let badgeImage = UIImage(named: "badge_icon_name_here")
let myBadgedImage: UIImage = drawImageView(mainImage!, withBadge: badgeImage!)
myImageView.image = myBadgedImage//set Image on ImageView
于 2016-04-06T06:10:22.090 回答
2
祝你好运
于 2016-04-12T16:10:26.140 回答
0
RDC 在 SWIFT 3 中的回答
func drawImageView(mainImage: UIImage, withBadge badge: UIImage) -> UIImage {
UIGraphicsBeginImageContextWithOptions(mainImage.size, false, 0.0)
mainImage.draw(in: CGRect(x: 0, y: 0, width: mainImage.size.width, height: mainImage.size.height))
badge.draw(in: CGRect(x: mainImage.size.width - badge.size.width, y: 0, width: badge.size.width, height: badge.size.height))
let resultImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return resultImage
}
于 2017-03-06T16:36:23.000 回答