Redimensionar a máscara ScaleAspectFit na Swift
Estou mascarando uma imagem em uma exibição de pilha e, por algum motivo estranho, minha máscara não está alinhando / redimensionando corretamente com a imagem.
Aqui está uma demonstração do que está acontecendo, enquanto adiciono instâncias dessa imagem dinamicamente em uma exibição de pilha, enquanto cada subvisão é redimensionada dentro de seus limites e espaçamento.
Como você pode ver, a máscara mantém o tamanho original da imagem e não a versão redimensionada. Eu tentei muitas variações diferentes de largura e altura, incluindo obounds.width, layer.frame.width, frame.width, frame.origin.x, etc, e não teve sorte.
Código atual no Swift 2:
let testPicture:UIImageView = UIImageView(image: UIImage(named: "myPicture"))
testPicture.contentMode = .ScaleAspectFit
testPicture.layer.borderWidth = 1
testPicture.clipsToBounds = true
testPicture.layer.masksToBounds = true
view.layer.addSublayer(shapeLayer)
var width = testPicture.layer.frame.width
var height = testPicture.layer.frame.height
let center = CGPointMake(width/2, height/2)
let radius = CGFloat(CGFloat(width) / 2)
// Mask
let yourCarefullyDrawnPath = UIBezierPath()
yourCarefullyDrawnPath.moveToPoint(center)
yourCarefullyDrawnPath.addArcWithCenter(center,
radius: radius,
startAngle: 0,
endAngle: CGFloat( (0.80*360.0) * M_PI / 180.0),
clockwise: true)
yourCarefullyDrawnPath.closePath()
let maskPie = CAShapeLayer()
maskPie.frame = testPicture.layer.bounds
testPicture.clipsToBounds = true
testPicture.layer.masksToBounds = true
maskPie.path = yourCarefullyDrawnPath.CGPath
testPicture.layer.mask = maskPie
// Add Into Stackview
self.myStackView.addArrangedSubview(testPicture)
self.myStackView.layoutIfNeeded()
Suspeito que estou buscando a largura e a altura incorretas para gerar as variáveis de centro e raio, embora depois de tentar todas as larguras e alturas diferentes que encontro, ainda não consiga obter os tamanhos corretos. :-(