UIImageView adjustsImageWhenAncestorFocused with rounded corners?

In tvOS all buttons and cells have rounded corners and the nice parallax effect. When you use UICollectionViews you can use a UIImageView as the focusable item by setting adjustsImageWhenAncestorFocused = true. When you do this you can us tvOS ImageStacks and the effect works.

However I can't seem to find where to specifiy that the corners should be rounded? Also the example project that Apple provides uses photos with no rounded corners. You can't use layer.cornerRadius because if the ImageView's property clipsToBounds is true the effect doesn't work anymore obviously.

Replies

ahufk i ran into the same problem so I process the images after downlaoding and cache them (recommened). It's the image's sublayer that gets the effect so you have to make sure clipToBounds (in UI land, or masksToBounds in CG land) is false or the effect will get masked out. I also tried to add sublayers to it so than a label would move in 3D with the image, but that didn't work.


Here is the process code:

    private func processImage(image: UIImage, withSize size: CGSize) -> UIImage {
        let layer = CALayer()
        layer.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: size)
        layer.cornerRadius = 10
        layer.masksToBounds = true
        layer.contentsGravity = "resizeAspectFill"
        layer.contents = image.CGImage
        UIGraphicsBeginImageContext(size)
        layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let processedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return processedImage
    }

I tried your approach and it's working for me.

Thanks!


EDIT: it rounds the corners, but it's not a good solution because if you take a rather big radius, you see the layer in the corners because that is not taken into account. I need something like the native tvOS corner radius on everything excpet the UIImageViews.

@ahufk have you managed to solve this? We are also facing this same issue. When you round the corners, you can see the "shine" still has a rectange shape.

I showed the issue to the Dev Evangelists at the Tech Talks in Toronto and they said it's a bug. It's reported and open rdar://23846376

Has this been fixed since? Any solution I don't know about? I still have this problem three years later.