是在swift ui中设置图像宽度和高度大小的任何方法。我找到了 frame 方法,但这不是我想要的。
这是我的快捷文件。
import SwiftUI
struct HeaderBar: View {
var body: some View {
VStack(alignment: .center, spacing: 0.0) {
HStack(alignment: .center) {
Spacer()
Image("jojologo-yellow")
.frame(width: 30.0, height: 30.0) //that is not the solution to change image size
.padding()
Spacer()
}
.background(Color.orange)
Spacer()
}
}
}
struct HeaderBar_Previews: PreviewProvider {
static var previews: some View {
HeaderBar()
}
}
如果我可以在Image中使用调整大小方法,那将很棒。例如
Image("jojologo-yellow").size(width: 30.0, height: 30.0)
所以,我的问题是“是否有任何方法可以在快速ui中调整图像的大小
答案 0 :(得分:4)
也许您正在寻找Image("jojologo-yellow").resizable().frame(width: 30.0, height: 30.0)
?
答案 1 :(得分:0)
您可以使用此功能调整图像大小;
X509Certificate
像这样打电话;
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
答案 2 :(得分:0)
在代码中添加.resizable()
修饰符
Image("yourImageName")
.resizable()
.scaledToFill() // add if you need
.frame(width: 30.0, height: 30.0) // as per your requirement
.clipped()