简体   繁体   English

如何从磁盘加载此图像数据并将其显示在我的 SwiftUI 列表中?

[英]How do I load this image data from disk and present it in my SwiftUI List?

I have used an imagePicker in SwiftUI to select an image from the user's photo album, and save this image to disk under a unique id.我在SwiftUI使用了一个imagePicker从用户的相册中选择了一个图像,并将这个图像以唯一的 id 保存到磁盘。 Here is saving the image (as Data):这是保存图像(作为数据):

 func saveImage(imageName: String, image: Data) {

guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

let fileName = imageName
let fileURL = documentsDirectory.appendingPathComponent(fileName)

//Checks if file exists, removes it if so.
if FileManager.default.fileExists(atPath: fileURL.path) {
    do {
        try FileManager.default.removeItem(atPath: fileURL.path)
        print("Removed old image")
    } catch let removeError {
        print("couldn't remove file at path", removeError)
    }

}

do {
    try image.write(to: fileURL)
    print("Image ID: \(imageName) saved. Data: \(image)")
} catch let error {
    print("error saving file with error", error)
}

}

After this, I want to populate a list, and convert the data back into an Image for use in a List :在此之后,我想填充一个列表,并将数据转换回Image以在List使用:

struct ListView: View {

//notification viewmodel class
@EnvironmentObject var vm: ViewModel
@State private var inputImage: UIImage?
@State private var image: Image?
@State private var id: String?

func loadImage() {
    guard let inputImage = inputImage else { return }
    image = Image(uiImage: inputImage)
}

func loadImageFromDiskWith(fileName: String) -> UIImage? {

  let documentDirectory = FileManager.SearchPathDirectory.documentDirectory

    let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
    let paths = NSSearchPathForDirectoriesInDomains(documentDirectory, userDomainMask, true)

    if let dirPath = paths.first {
        let imageUrl = URL(fileURLWithPath: dirPath).appendingPathComponent(fileName)
        let image = UIImage(contentsOfFile: imageUrl.path)
        return image

    }

    return nil
}

var body: some View {
    
    NavigationView {

        List {
            
            //New row for each item in notification array
            ForEach(vm.notificationArray, id: \.content) { notification in
                
                HStack {
                VStack(alignment: .leading, spacing: 10) {
                    //Populate text with info from notification.
                    Text(notification.content.title)
                    let id = notification.identifier
                
                  //I want to call loadImage here, and show the image if it exists, but there are many errors with this and I can't seem to get it to work
                    image = Image(uiImage: loadImageFromDiskWith(fileName: id)!)
            
                    if image != nil {
                        image?
                            .resizable()
                            .scaledToFit()
                    } else {
                    }
                    
            }
         
            }
            }
            
            
    }
    
}
  
    }
}

}

How can I call my load image function and get present the image in the list?如何调用我的加载图像功能并在列表中显示图像? Thank you.谢谢你。

Try the following尝试以下

VStack(alignment: .leading, spacing: 10) {
    //Populate text with info from notification.
    Text(notification.content.title)

    if let image = loadImageFromDiskWith(fileName: notification.identifier) {
        Image(uiImage: image)
            .resizable()
            .scaledToFit()
    } else {
       // Put some placeholder image here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM