【发布时间】:2019-10-23 15:10:37
【问题描述】:
我在 Assets.xcassets 中有一张大图。如何使用 SwiftUI 调整此图像的大小以使其变小?
我尝试设置框架但它不起作用:
Image(room.thumbnailImage)
.frame(width: 32.0, height: 32.0)
【问题讨论】:
我在 Assets.xcassets 中有一张大图。如何使用 SwiftUI 调整此图像的大小以使其变小?
我尝试设置框架但它不起作用:
Image(room.thumbnailImage)
.frame(width: 32.0, height: 32.0)
【问题讨论】:
你也可以使用:
Image("Example")
.scaleEffect(NumberWithSizeBetweenZeroAndOne)
【讨论】:
你需要添加.resizable修饰符,才能改变图片的大小
代码如下所示:
Image(room.thumbnailImage)
.resizable()
.frame(width: 32.0, height: 32.0)
【讨论】:
图片(room.thumbnailImage) .frame(宽度: 32.0, 高度: 32.0)
添加可调整大小和框架
.resizable() .frame(maxWidth: 600, maxHeight: 300,)
【讨论】:
Image(systemName: "person.fill")
.font(.system(size: 13))
如果您使用systemName,也可以使用。
【讨论】:
建议使用以下代码匹配多种屏幕尺寸:
Image("dog")
.resizable()
.frame(minWidth: 200, idealWidth: 400, maxWidth: 600, minHeight: 100, idealHeight: 200, maxHeight: 300, alignment: .center)
【讨论】:
为了使图像缩放以适应当前视图,我们使用 resizable() 修饰符,它可以调整图像大小以适应可用空间。
例如:
Image("ImageName")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 200, height: 200, alignment: .center)
【讨论】:
这个怎么样:
struct ResizedImage: View {
var body: some View {
Image("myImage")
.resizable()
.scaledToFit()
.frame(width: 200, height: 200)
}
}
图像视图为 200x200,但图像保持原始纵横比(在该帧内重新缩放)。
【讨论】:
Image(uiImage: image!).resizable().aspectRatio(image!.size, contentMode: .fill) 其中image 的类型为UIImage,因为Image 类型不公开任何大小属性。
您可以使用 resizable() 属性,但请记住,您不能在通用修饰符中使用 resizable,因此您必须使用图像扩展来实现它。
extension Image {
func customModifier() -> some View {
self
.resizable()
.aspectRatio(contentMode: .fit)
}
【讨论】:
在图像名称后使用.resizable() 方法。
确保.resizable()的使用需要在任何修改之前声明。
像这样:
Image("An Image file name")
.resizable()
//add other modifications here
【讨论】:
在对Image 应用任何大小修改之前,您应该使用.resizable()。
Image(room.thumbnailImage)
.resizable()
.frame(width: 32.0, height: 32.0)
【讨论】:
aspectRatio(_:contentMode:)
Image("name").resizable().scaledToFit() 并没有被窃听。因此,您可以将图像包裹在视图中,将视图的框架调整为您需要的任何大小,然后scaledToFit() 将在保持纵横比的同时使图像尽可能大。
在 SwiftUI 中,使用 .resizable() 方法调整图像大小。通过使用.aspectRatio() 并指定ContentMode,您可以根据需要“适合”或“填充”图像。
例如,下面是通过拟合调整图像大小的代码:
Image("example-image")
.resizable()
.aspectRatio(contentMode: .fit)
【讨论】:
默认情况下,图像视图会根据内容自动调整大小,这可能会使它们超出屏幕范围。如果添加 resizable() 修饰符,则图像将自动调整大小以填充所有可用空间:
Image("example-image")
.resizable()
但是,这也可能导致图像的原始纵横比失真,因为它将在所有维度上被拉伸到填充空间所需的任何量。
如果你想保持它的纵横比,你应该使用 .fill 或 .fit 添加一个 aspectRatio 修饰符,如下所示:
Image("example-image")
.resizable()
.aspectRatio(contentMode: .fit)
【讨论】:
另一种方法是使用scaleEffect 修饰符:
Image(room.thumbnailImage)
.resizable()
.scaleEffect(0.5)
【讨论】:
注意:我的图像名称是
img_Logo,您可以更改图像名称定义图像属性:
VStack(alignment: .leading, spacing: 1) {
//Image Logo Start
Image("img_Logo")
.resizable()
.padding(.all, 10.0)
.frame(width: UIScreen.main.bounds.width * 0.4, height: UIScreen.main.bounds.height * 0.2)
//Image Logo Done
}
【讨论】:
struct AvatarImage: View {
var body: some View {
Image("myImage")
.resizable()
.scaledToFill() // <=== Saves aspect ratio
.frame(width: 60.0, height:60)
.clipShape(Circle())
}
}
【讨论】:
理解代码的逻辑结构非常重要。与 SwiftUI 一样,默认情况下图像不可调整大小。因此,要调整任何图像的大小,您必须在声明图像视图后立即应用 .resizable() 修饰符使其可调整大小。
Image("An Image file name")
.resizable()
【讨论】:
您可以如下定义图像属性:-
Image("\(Image Name)")
.resizable() // Let you resize the images
.frame(width: 20, height: 20) // define frame size as required
.background(RoundedRectangle(cornerRadius: 12) // Set round corners
.foregroundColor(Color("darkGreen")) // define foreground colour
【讨论】:
因为我们不应该硬编码/修复图像大小。这是提供范围以根据不同设备上的屏幕分辨率进行调整的更好方法。
Image("ImageName Here")
.resizable()
.frame(minWidth: 60.0, idealWidth: 75.0, maxWidth: 95.0, minHeight: 80.0, idealHeight: 95.0, maxHeight: 110.0, alignment: .center)
.scaledToFit()
.clipShape(Capsule())
.shadow(color: Color.black.opacity(5.0), radius: 5, x: 5, y: 5)
【讨论】:
嗯,似乎在 SwiftUI 中非常简单/按照他们提供的演示:https://developer.apple.com/videos/play/wwdc2019/204
struct RoomDetail: View {
let room: Room
var body: some View {
Image(room.imageName)
.resizable()
.aspectRatio(contentMode: .fit)
}
希望对您有所帮助。
【讨论】:
如果您想在 swiftUI 中调整图像大小,只需使用以下代码:
import SwiftUI
struct ImageViewer : View{
var body : some View {
Image("Ssss")
.resizable()
.frame(width:50,height:50)
}
}
但是这里有问题。 如果您将此图像添加到按钮中,则不会显示该图像,只会显示一个蓝色块。 要解决这个问题,只需这样做:
import SwiftUI
struct ImageViewer : View{
var body : some View {
Button(action:{}){
Image("Ssss")
.renderingMode(.original)
.resizable()
.frame(width:50,height:50)
}
}
}
【讨论】:
扩展@rraphael 的答案和 cmets:
从 Xcode 11 beta 2 开始,您可以将图像缩放到任意尺寸,同时通过将图像包裹在另一个元素中来保持原始纵横比。
例如
struct FittedImage: View
{
let imageName: String
let width: CGFloat
let height: CGFloat
var body: some View {
VStack {
Image(systemName: imageName)
.resizable()
.aspectRatio(1, contentMode: .fit)
}
.frame(width: width, height: height)
}
}
struct FittedImagesView: View
{
private let _name = "checkmark"
var body: some View {
VStack {
FittedImage(imageName: _name, width: 50, height: 50)
.background(Color.yellow)
FittedImage(imageName: _name, width: 100, height: 50)
.background(Color.yellow)
FittedImage(imageName: _name, width: 50, height: 100)
.background(Color.yellow)
FittedImage(imageName: _name, width: 100, height: 100)
.background(Color.yellow)
}
}
}
结果
(由于某种原因,图像显示有点模糊。请放心,实际输出很清晰。)
【讨论】:
.aspectRatio(1, ...。并不是说到目前为止这里的任何其他解决方案都对我有用......
如果您想在调整大小时使用 纵横比,则可以使用以下代码:
Image(landmark.imageName).resizable()
.frame(width: 56.0, height: 56.0)
.aspectRatio(CGSize(width:50, height: 50), contentMode: .fit)
【讨论】: