当我尝试在watchOS应用程序中使用import WatchKit
struct ImageGenerator() {
func image() -> UIImage {
let format = UIGraphicsImageRendererFormat() // ERROR
format.scale = 1
format.opaque = true
let renderer = UIGraphicsImageRenderer(size: size, format: format) // ERROR
let image = renderer.image { imageRendererContext in
// ...
}
}
}
时,编译器会抛出错误。 entry in the documentation看起来只在iOS和tvOS中可用。你知道为什么会这样吗?
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
nav{
background-color: #000;
text-align: center;
padding: 8px;
}
nav li{
display: inline-block;
margin: 5px 40px 5px 40px;
padding: 3px;
font-size: 40px;
}
body{
padding: 16px;
margin: 10px;
background-color: #FFE4C4;
}
nav li a{
color: white;
text-decoration: none;
font-family: sans-serif;
答案 0 :(得分:2)
UIGraphicsImageRenderer
。但是,您仍然可以在watchOS上使用旧版渲染API:
func image() -> UIImage {
UIGraphicsBeginImageContextWithOptions(yourImageSize, isOpaque, scale)
defer { UIGraphicsEndImageContext() }
let context = UIGraphicsGetCurrentContext()!
// draw your image at here...
return UIGraphicsGetImageFromCurrentImageContext()! // get image
}