无法绘制半径正确的圆圈UIBezierPath ios / swift

时间:2016-04-11 20:38:22

标签: ios swift uibezierpath

当我计算半径时,UIImageView的宽度除以2 我得到了。

在故事板中,我的UIImageView的大小是64x64

with a radius computed with a width / 2

这是我的代码(Image是我用于FastImageCash库的自定义类):

    let image = Image(identifier: fileinfo.identifier, weburl: weburl, fileurl: fileinfo.fileurl)
    image.loadImage()

    let rect = CGRectMake(0.0, 0.0, imgUser.frame.size.width, imgUser.frame.size.height)

    let sizeImage = image.img!.size ?? CGSizeZero

    let ratioW = sizeImage.width / rect.width
    let ratioH = sizeImage.height / rect.height

    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)

    let radius = CGFloat(rect.width / 2.0)

    let circle = UIBezierPath(arcCenter: CGPointMake(rect.width / 2, rect.height / 2), radius: radius, startAngle: CGFloat(0.degreesToRadians), endAngle: CGFloat(360.degreesToRadians), clockwise: true)

    circle.lineWidth = CGFloat(1.0)

    circle.addClip()

    var area = CGRect()

    let ratio = ratioW > ratioH ? ratioW : ratioH

    area.size.height = sizeImage.height / ratio
    area.size.width = sizeImage.width / ratio
    area.origin.x = (rect.height - area.size.height) / 2
    area.origin.y = (rect.width - area.size.width) / 2

    image.img?.drawInRect(area)

    let thumb = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    imgUser.image = thumb

如果用

计算半径,我不明白为什么它有用
    ...
    let radius = CGFloat(rect.width / 2.5)
    ...

0 个答案:

没有答案