我构建了UIImageView
扩展名以使用其中的图片并为其添加徽章。但我真的无法做到正确,因为在那之后我正在绕UIImageView
角半径。我想要的是在图像的UIImageView
角上显示徽章。
我就是这样做的:
public extension UIImageView {
func addBadgeRightBottom (withBadge badge: UIImage) {
if self.image != nil {
UIGraphicsBeginImageContextWithOptions(self.frame.size, false, 0.0)
self.image?.draw(in: CGRect(x: 0, y: 0, width: (self.frame.size.width), height: (self.frame.size.height)))
badge.draw(in: CGRect(x: (self.frame.size.width) - badge.size.width, y: (self.frame.size.height) - badge.size.height, width: badge.size.width, height: badge.size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
DispatchQueue.main.async() { () -> Void in
self.image = image
}
UIGraphicsEndImageContext()
}
}
}
这就是我得到的:
我真正想要的是:
答案 0 :(得分:2)
最后,我通过在另一个圆UIImageView
上创建了我想要的相同位置UIImage
徽章UIImageView
的新UICollectionViewCell
,并将其作为子视图添加到我的{ {1}}因为这些个人资料图片图片位于UICollectionViewCell
不幸的是,毕竟我并没有将其作为扩展。
我是怎么做到的。
初始化UIImageView
类
CelebCollectionViewCell
后
var celebBadgeImageView = UIImageView()
我初始化徽章的CGRect
并将其添加为子视图
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: CelebCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "celebCell", for: indexPath) as! CelebCollectionViewCell
cell.celebBadgeImageView.frame = CGRect(x: cell.celebImage.frame.size.width - (#imageLiteral(resourceName: "verifiedCelebrity").size.width/2), y: cell.celebImage.frame.size.height - #imageLiteral(resourceName: "verifiedCelebrity").size.height, width: #imageLiteral(resourceName: "verifiedCelebrity").size.width, height: #imageLiteral(resourceName: "verifiedCelebrity").size.height)
cell.celebBadgeImageView.image = #imageLiteral(resourceName: "verifiedCelebrity")
cell.addSubview(cell.celebBadgeImageView)
cell.celebImage.image = #imageLiteral(resourceName: "profile_user.jpg")
cell.celebImage.circularView()
return cell
}