一對一直播app開發,單次獲得或上傳一張相簿裡的照片

zhibo系統開發發表於2022-02-18

一對一直播app開發,單次獲得或上傳一張相簿裡的照片實現的相關程式碼

SwiftUI沒辦法自己獲取系統相簿內容,所以得依靠一下UIKit,標頭檔案如下:

import SwiftUI
import UIKit

首先我們建立一個ImagePicker結構體供我們獲取系統相簿內容:

struct ImagePicker: UIViewControllerRepresentable {
    @Environment(\.presentationMode) private var presentationMode
    let sourceType: UIImagePickerController.SourceType
    let onImagePicked: (UIImage) -> Void
 
    final class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
 
        @Binding private var presentationMode: PresentationMode
        private let sourceType: UIImagePickerController.SourceType
        private let onImagePicked: (UIImage) -> Void
 
        init(presentationMode: Binding<PresentationMode>,
             sourceType: UIImagePickerController.SourceType,
             onImagePicked: @escaping (UIImage) -> Void) {
            _presentationMode = presentationMode
            self.sourceType = sourceType
            self.onImagePicked = onImagePicked
        }
 
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            let uiImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
            onImagePicked(uiImage)
            presentationMode.dismiss()
        }
 
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            presentationMode.dismiss()
        }
 
    }
 
    func makeCoordinator() -> Coordinator {
        return Coordinator(presentationMode: presentationMode,
                           sourceType: sourceType,
                           onImagePicked: onImagePicked)
    }
 
    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        let picker = UIImagePickerController()
        picker.sourceType = sourceType
        picker.delegate = context.coordinator
        return picker
    }
 
    func updateUIViewController(_ uiViewController: UIImagePickerController,
                                context: UIViewControllerRepresentableContext<ImagePicker>) {
    }
}


然後我們就可以很簡單地從系統相簿中獲取照片了:

struct ContentView: View {
    @State private var showImagePicker = false
    //這裡的image用於放置等會獲取的照片
    @State private var image: UIImage = UIImage()
    var body: some View {
        List{
            Button(action: {
                showImagePicker = true
            }, label: {
                Text("Select Image")
            })
            
            Image(uiImage: image)
                .resizable()
                .aspectRatio(contentMode: .fit)
        }
        .sheet(isPresented: $showImagePicker,
               content: {
            ImagePicker(sourceType: .photoLibrary) { image in
                self.image = image
            }
        })
    }
}


以上就是一對一直播app開發,單次獲得或上傳一張相簿裡的照片實現的相關程式碼, 更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2856345/,如需轉載,請註明出處,否則將追究法律責任。

相關文章