如何使用UIGraphicsGetImageFromCurrentImageContext渲染的图像视图为所有角设置角半径

时间:2016-05-23 07:27:53

标签: ios swift

以前的表现如下:

enter image description here

在我生成拖动视图后:

enter image description here

这就是我从单元格生成拖动视图的方法:

private func setupDraggingViewForCell(cell: BWStudentWishlistBookCollectionViewCell) {

    UIGraphicsBeginImageContextWithOptions(cell.bounds.size, false, 0)
    cell.bookImageView.layer.renderInContext(UIGraphicsGetCurrentContext()!)

    draggingView = UIImageView(image: UIGraphicsGetImageFromCurrentImageContext())
    draggingView?.clipsToBounds = true
    draggingView?.contentMode = .ScaleAspectFit
    draggingView?.layer.masksToBounds = true
    draggingView?.layer.cornerRadius = 10

    view.addSubview(draggingView!)

    UIGraphicsEndImageContext()
}
  

如您所见,只有一个角落是圆形的。为什么呢?

3 个答案:

答案 0 :(得分:0)

您可以将clipsToBounds设置为false,并且可能会解决您案例中的问题。否则它会显示封面的完整框架,然后你可以决定做什么。

draggingView?.clipsToBounds = false

答案 1 :(得分:0)

您可以根据需要直接将此代码添加到自定义单元格中.......

override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        imageView.layer.shadowColor = UIColor.blackColor().CGColor
        imageView.layer.shadowOpacity = 1
        imageView.layer.cornerRadius = 5
        imageView.layer.shadowOffset = CGSizeZero
        imageView.layer.shadowRadius = 2

 //If you added Aspect to Fill 
        imageView.clipsToBounds = true
    }

我希望这会对你有帮助......(Y)

答案 2 :(得分:-1)

不知道但是尝试围绕图像而不是imageView

func makeRoundedImage(image: UIImage, radius: Float) -> UIImage {
    var imageLayer: CALayer = CALayer.layer
    imageLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height)
    imageLayer.contents = (image.CGImage as! AnyObject)
    imageLayer.masksToBounds = true
    imageLayer.cornerRadius = 10
    UIGraphicsBeginImageContext(image.size)
    imageLayer.renderInContext(UIGraphicsGetCurrentContext())
    var roundedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return roundedImage
}

然后将图像设置为图像视图。并清除dragableView的背景颜色

希望它会有所帮助..!