How to resize Image with SwiftUI?

前端 未结 14 508
清歌不尽
清歌不尽 2020-12-23 19:54

I have a large image in Assets.xcassets. How to resize this image with SwiftUI to make it small?

I tried to set frame but it doesn\'t work:

Image(roo         


        
相关标签:
14条回答
  • 2020-12-23 20:32

    Well, It's seems pretty easy in SwiftUI / Following the demo they given : 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)
     }
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-23 20:34

    Expanding on @rraphael's answer and comments:

    As of Xcode 11 beta 2, you can scale an image to arbitrary dimensions, while maintaining the original aspect ratio by wrapping the image in another element.

    e.g.

    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)
    
            }
        }
    }
    

    Results

    (For some reason, the image is showing as a bit blurry. Rest assured that the real output is sharp.)

    0 讨论(0)
  • 2020-12-23 20:34

    If you want to resize the image in swiftUI just use the following code :

    import SwiftUI
    
        struct ImageViewer : View{
            var body : some View {
                Image("Ssss")
                .resizable()
                .frame(width:50,height:50)
            }
        }
    

    But here is problem with this. If you add this Image inside a Button, the Image will not be shown, just a block of blue colour would be there. To solve this issue, just do this :

    import SwiftUI
    
    struct ImageViewer : View{
        var body : some View {
            Button(action:{}){
            Image("Ssss")
            .renderingMode(.original)
            .resizable()
            .frame(width:50,height:50)
        }
       }
    }
    
    0 讨论(0)
  • 2020-12-23 20:37
    struct AvatarImage: View {
        var body: some View {
    
                Image("myImage")
                    .resizable()
                    .scaledToFill() // <=== Saves aspect ratio
                    .frame(width: 60.0, height:60)
                    .clipShape(Circle())
    
        }
    }
    
    0 讨论(0)
  • 2020-12-23 20:39

    It is very important to understand logical structure of code. Like in SwiftUI an image is not resizable by default. Thus, to resize any image you have to make it resizable by applying the .resizable() modifier immediately after you declare an Image view.

    Image("An Image file name")
        .resizable()
    
    0 讨论(0)
  • 2020-12-23 20:42

    Another approach is to use the scaleEffect modifier:

    Image(room.thumbnailImage)
        .resizable()
        .scaleEffect(0.5)
    
    0 讨论(0)
提交回复
热议问题