我也可以像 xcassets 一样在源代码中切片图像资源吗?
Could I also slice image resource in source code like xcassets?
我想直接在源代码上切片图像,就像在 xcassets 上切片图像的方法一样,如下图所示。
我已经在 google 上找到了方法,但我做不到。
如果你知道那是怎么回事,请告诉我吗?谢谢!
当您通过故事板更改图像的切片时,它实际上会将更改应用到与图像对应的 Contents.json
文件。
应用您在附加图像中所做的更改,以下更改将应用于文件:
"resizing" : {
"mode" : "9-part",
"center" : {
"mode" : "tile",
"width" : 1,
"height" : 1
},
"cap-insets" : {
"bottom" : 27,
"top" : 26,
"right" : 21,
"left" : 21
}
}
您可以通过使用 UIImage
方法以编程方式应用相同的更改 resizableImage
:
Declaration:
func resizableImage(withCapInsets capInsets: UIEdgeInsets,
resizingMode: UIImage.ResizingMode) -> UIImage
用法示例:
let image = UIImage(named: "example.png")
let insets = UIEdgeInsets(top: 1, left: 2, bottom: 1, right: 2)
let imageWithInsets = image.resizableImage(withCapInsets: insets, resizingMode: .tile)
我想直接在源代码上切片图像,就像在 xcassets 上切片图像的方法一样,如下图所示。 我已经在 google 上找到了方法,但我做不到。 如果你知道那是怎么回事,请告诉我吗?谢谢!
当您通过故事板更改图像的切片时,它实际上会将更改应用到与图像对应的 Contents.json
文件。
应用您在附加图像中所做的更改,以下更改将应用于文件:
"resizing" : {
"mode" : "9-part",
"center" : {
"mode" : "tile",
"width" : 1,
"height" : 1
},
"cap-insets" : {
"bottom" : 27,
"top" : 26,
"right" : 21,
"left" : 21
}
}
您可以通过使用 UIImage
方法以编程方式应用相同的更改 resizableImage
:
Declaration:
func resizableImage(withCapInsets capInsets: UIEdgeInsets, resizingMode: UIImage.ResizingMode) -> UIImage
用法示例:
let image = UIImage(named: "example.png")
let insets = UIEdgeInsets(top: 1, left: 2, bottom: 1, right: 2)
let imageWithInsets = image.resizableImage(withCapInsets: insets, resizingMode: .tile)