其他分享
首页 > 其他分享> > SwiftUI3.0 Image的使用相当于UIImageView控件

SwiftUI3.0 Image的使用相当于UIImageView控件

作者:互联网

//
//  ContentView.swift
//  ImageControl
//
//  Created by lujun on 2021/12/19.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        //        simpleImage
        //        basicUsage
//        initWithBundle2
//        initWithIcon
//        resize
//        renderMode
        remoteImage
    }
    
    var simpleImage: some View {
        Text("smiple Image smiple Image smiple Image smiple Image smiple Image smiple Image smiple Image")
            .multilineTextAlignment(.center)
            .padding()
    }
    
    var basicUsage: some View {
        Image("123")
            .resizable()
            .aspectRatio(contentMode: .fit)
            .scaledToFit()
    }
    
    //MARK: - bundle
    var initWithBundle: some View {
        Group {
            if let path = Bundle.main.path(forResource: "Lujun", ofType: "bundle"),
               let restBu = Bundle(path: path),
               let uiImage = UIImage(named: "123.jpeg",in: restBu,with: nil) {
                Image(uiImage: uiImage)
                    .resizable()
                    .scaledToFit()
            }else{
                Color.red
            }
            
        }
    }
    
    var initWithBundle2: some View {
        VStack{ () -> AnyView in
            guard let path = Bundle.main.path(forResource: "Lujun", ofType: "bundle"),
                  let restBu = Bundle(path: path),
                  let uiImage = UIImage(named: "123.jpeg",in: restBu,with: nil) else  {
                      return AnyView(Color.red)
                  }
            return AnyView(
                Image(uiImage: uiImage)
                    .resizable()
                    .scaledToFit()
            )
            
        }
        
    }
    
    
    var initWithIcon: some View {
        VStack(spacing: 10) {
            Image(systemName: "bookmark.circle.fill")
                .font(.title)
            HStack{
                HStack(spacing: 0){
                    Text("123")
                    Image(systemName: "doc")
                    Text("456")
                }
                .font(.system(size: 34, weight: .bold))
                Text("123\(Image(systemName: "doc"))567")
                .font(.system(size: 34, weight: .bold))
            }
            Image("123")
                .resizable()
                .frame(width: 100, height: 100)
        }
    }
    
    
    var resize: some View {
        
//        Image("12")
//            .resizable( resizingMode: .stretch)
//            .edgesIgnoringSafeArea(.all)
        
//        Image("12")
//            .resizable()
//            .scaledToFit()
//        Image("12")
//            .resizable(capInsets: .init(top: 10, leading: 10, bottom: 10, trailing: 10), resizingMode: .stretch)
//            .ignoresSafeArea()
        
        Image("12")
//            .interpolation(.none)
//            .antialiased(true)
            .resizable()
            .frame(width: 300, height: 300)
            .clipped()
    }
    
    var renderMode: some View {
        TabView {
            Text("1")
                .tabItem{
                    Image("btn_home_selected")
                        .renderingMode(.original)
                    Text("首页")
                }
            Text("2")
                .tabItem{
                    Image("btn_home_selected")
                        .renderingMode(.original)
                    Text("首页")
                }
            Text("3")
                .tabItem{
                    Image("btn_home_selected")
                        .renderingMode(.original)
                    Text("首页")
                }
                
        }
        
    }
    
    
    var remoteImage: some View {
        Text("123")
    }
    
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

标签:控件,resizable,Text,SwiftUI3.0,some,UIImageView,var,Image,View
来源: https://www.cnblogs.com/Johnson-Swift/p/15770674.html