如何在  Watch 应用程序中圆化 WKInterfaceImage 的角?

How to round WKInterfaceImage's corners in an  Watch app?

有很多  Watch 应用程序的 WKInterfaceImages 有圆角。我试图在我的测试应用程序中舍入一些 WKInterfaceImages,但我不明白该怎么做。

我无法使用 imageView.layer。 ...与普通 iPhone 应用程序一样,我找不到使用代码或情节提要来执行此操作的替代方法。

我必须屏蔽所有 PNG 还是有更简单的方法?

你是对的 CALayerUIView 不能直接在 watchOS 2 上使用。但是你可以使用图形功能,例如 this approach 在 Watch 上是完全可以接受的。

Swift中的类似物:

class ImageTools {
    class func imageWithRoundedCornerSize(cornerRadius:CGFloat, usingImage original: UIImage) -> UIImage {
        let frame = CGRectMake(0, 0, original.size.width, original.size.height)

        // Begin a new image that will be the new image with the rounded corners
        UIGraphicsBeginImageContextWithOptions(original.size, false, 1.0)

        // Add a clip before drawing anything, in the shape of an rounded rect
        UIBezierPath(roundedRect: frame, cornerRadius: cornerRadius).addClip()

        // Draw the new image
        original.drawInRect(frame)

        // Get the new image
        let roundedImage = UIGraphicsGetImageFromCurrentImageContext()

        // Lets forget about that we were drawing
        UIGraphicsEndImageContext()

        return roundedImage
    }
}

在你的某处 WKInterfaceController class:

let originalImage = UIImage(named: "original-image")!
let roundedImage = ImageTools.imageWithRoundedCornerSize(60, usingImage: originalImage)

// Set `UIImage` for your `WKInterfaceImage`
imageOutlet.setImage(roundedImage)

我解决了从情节提要中删除 WKInterfaceImage 然后用 WKInterfaceGroup 替换它的问题,我将其设置为与先前图像相同的大小然后,从属性检查器中,我设置了他的半径(是的,可以使用组!)然后我声明组在控制器中并使用 row.flagView.setBackgroundImageNamed(imageName).

设置图像