0

When i compute the radius with the width of the UIImageView divide by 2 i obtain that.

In the storyboard the size of my UIImageView is 64x64

with a radius computed with a width / 2

Here s my code (Image is my custom class used with the library 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

I don't understand why it works if i compute the radius with

    ...
    let radius = CGFloat(rect.width / 2.5)
    ...
  • 1
    Because that code is in the wrong place - the layout information is not present yet - do this code *once* in viewDidLayoutSubviews or something similiar. Or even better: add all your different objects to the view in `viewDidLoad` - and then layout them in already mentioned method. – luk2302 Apr 11 '16 at 21:17
  • I don t understand . This code is executed after a tap on a button, it s not built at launch (viewDidLoad) of the viewcontroller. – Kevin Launay Apr 12 '16 at 14:34

0 Answers0