gpt4 book ai didi

ios - 使用文件管理器创建文件时如何在 UI 中显示事件指示器

转载 作者:行者123 更新时间:2023-11-29 05:32:52 32 4
gpt4 key购买 nike

我正在从远程服务器下载文件(.pdf、.JPG、.PNG 等)。 “fullFileURLString”是以字符串形式提供文件路径 URL。

当用户从 Collection View 中单击文件名时,我需要提供将文件保存在用户自己的设备中的选项。为此,我正在使用 UIDocumentInteractionController()。我计划向用户提供此功能。

此功能运行正常。这个过程需要一些时间才能完成。我想在这个过程发生时显示事件指示器。但事件指示器没有动画或没有显示在 View 上。

如何在该过程发生时显示事件指示器并在 documentInteractionController 存在时隐藏事件指示器?

我尝试过下面的代码

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"showUplodedMediaCellCollectionViewCell", for: indexPath) as! showUplodedMediaCellCollectionViewCell;
if let bytes = Memory().deviceRemainingFreeSpaceInBytes() {
let twoHundresMb:Int64 = 283115520
if bytes < twoHundresMb {
Alert.showAlertView(on: self, message: "There is not enough available storage to download.")
}
else {
let activityIndicatior:UIActivityIndicatorView = UIActivityIndicatorView()
activityIndicatior.center = self.view.center
activityIndicatior.hidesWhenStopped = true
activityIndicatior.style = .gray
view.addSubview(activityIndicatior)
activityIndicatior.startAnimating()
let mediaDetails = MediaFiles?[indexPath.row]
if let path = mediaDetails?.folderPath {
if let Filename = mediaDetails?.name {
let fullFileURLString = "\(baseURL)"+"\(path)"+"\(Filename)"
if let FileUrl = URL(string: fullFileURLString) {
let Filedata = try? Data(contentsOf: FileUrl)
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let documentDirectoryPath2 = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let documentDirectoryPathURL = NSURL(fileURLWithPath: documentDirectoryPath)

if let pathComponent = documentDirectoryPathURL.appendingPathComponent(Filename) {
let fileManager = FileManager.default
let filePath = pathComponent.path
if fileManager.fileExists(atPath: filePath) {
print("Already Have:",Filename)
}
else {
print("No available",Filename)
let fileManager = FileManager.default
let fileManagerPath = documentDirectoryPath2.appendingPathComponent(Filename)
fileManager.createFile(atPath: fileManagerPath, contents: Filedata, attributes: nil)
print("path:- \(fileManagerPath)")
}
}

let destinationPath = documentDirectoryPath2.appendingPathComponent(Filename)
let destinationURL = URL(fileURLWithPath: destinationPath)
let dowanloadedMediaDocumentDirectoryPathURL = NSURL(fileURLWithPath: documentDirectoryPath)

if let pathComponent = dowanloadedMediaDocumentDirectoryPathURL.appendingPathComponent(Filename) {
let fileManager = FileManager.default
let filePath = pathComponent.path
if fileManager.fileExists(atPath: filePath) {
activityIndicatior.stopAnimating()
var documentInteractionController = UIDocumentInteractionController()
documentInteractionController.url = destinationURL
if !documentInteractionController.presentOpenInMenu(from: cell.bounds, in: view, animated: true) {
print("You don't have an app installed that can handle ePub files.")

}
}
else {
Alert.showAlertView(on: self, message: "Unable to download file from remote server.")
}
}
}
}
}
}
}
else {
Alert.showAlertView(on: self, message: "There is not enough available storage to download.")
}
}

我希望 ActivityIndi​​cator 会一直显示动画,直到 documentInteractionController 不再出现。

最佳答案

您应该将所有下载文件过程放在后台线程中。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

// Some code here

DispatchQueue.main.async {
activityIndicatior.startAnimating()
}

DispatchQueue.global(qos: .userInitiated).async {
// Download file or perform expensive task

DispatchQueue.main.async {
activityIndicator.stopAnimating()
}
}
}

将您的代码拆分为具有单一职责的函数,这将使您的代码更易于调试。

关于ios - 使用文件管理器创建文件时如何在 UI 中显示事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57357267/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com