我有这段代码:
struct MyPackageData: Codable {
let versionDate: String
let photosSizes, packshotsSizes, inspirationsPhotoSizes, conceptsSlidesSizes: [String: Int]
let tipsSlidesSizes, leafletsSlidesSizes, leafletsPdfSlidesSizes: [String: Int]
}
func downloadImagesTipsSlides(toDownloads: MyPackageData, savedURL: String){
do {
let dateToParse = toDownloads.tipsSlidesSizes
for photo in dateToParse {
let fileNamePhoto = photo.key
let checkLocalCopyFileIsAvailable = checkLocalFileCopyIsAvailable(fileName: (AppGlobalManager.sharedManager.loggedUser?.selectedLanguage)! + "/" + FileFolders.GET_TIPS_SLIDES.rawValue + "/" + "\(fileNamePhoto)")
if checkLocalCopyFileIsAvailable == false {
let productImageUrl : URL = URL(string: "\(ApiConstans.fullPath)?action=\(FileFolders.GET_TIPS_SLIDES.rawValue)&name=\(fileNamePhoto)&resolution=FHD&lang=" + (AppGlobalManager.sharedManager.loggedUser?.getUserLanguageUrl())!)!
remoteResource(at: productImageUrl, fileSize: photo.value) { (isImage) in
if isImage == false {
}
if isImage == true {
self.saveDownloadImages(fileInternetUrl: productImageUrl, fileName: "\(fileNamePhoto)", savedURL: savedURL)
}
}
} else {
print("I have photo \(fileNamePhoto) - nie robię nic")
}
}
}
}
func remoteResource(at url: URL, fileSize: Int, isImage: @escaping ((Bool) -> Void)) {
let request = URLRequest(url: url)
debugPrint(request)
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let data = data, let image = UIImage(data: data) {
if let _ = UIImageJPEGRepresentation(image, 1.0) {
isImage(true)
} else if let _ = UIImagePNGRepresentation(image) {
isImage(true)
} else {
isImage(false)
}
} else {
isImage(false)
}
isImage(true) // remove
}
task.resume()
}
func saveDownloadImages(fileInternetUrl: URL, fileName: String, savedURL: String){
let documentsDir = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
let fullImagesPath = documentsDir.appendingPathComponent(savedURL)
let cms = ServerConnect()
print("XX \(fullImagesPath)")
cms.downloadedFileFromInternet(fileInternetUrl: fileInternetUrl, saveToPath: fullImagesPath, fileName: fileName, completion: { (data) in
switch data {
case .succes:
print("")
case .error(let error):
//self.errorLoginMessage(txt: "MainView - Error 110: Problem with download images. \(error)", title: "Blad".localized())
print("")
break
}
})
}
当我出错时:线程7:致命错误:在解开可选值时意外发现nil
排队:
let productImageUrl : URL = URL(string: "\(ApiConstans.fullPath)?action=\(FileFolders.GET_TIPS_SLIDES.rawValue)&name=\(fileNamePhoto)&resolution=FHD&lang=" + (AppGlobalManager.sharedManager.loggedUser?.getUserLanguageUrl())!)!
我的调试控制台:
我的文件名有问题: - wolnośćogłoszenia.jpg - Wydajnośćwagowa___ 004.jpg
等
使用正常名称的文件名:1.jpg,139485.jpg,14383.jpg - 此代码可正常工作。
以上代码是从互联网上下载照片并将其保存到设备的内存中。没有特殊名称的文件可以正常工作。
我无法更改服务器上的文件名。
有谁知道如何修复它?
答案 0 :(得分:1)
不要强行解开你会拯救这么多常见事故的东西。
let productImageUrl : URL = URL(string: "\(ApiConstans.fullPath)?action=\(FileFolders.GET_TIPS_SLIDES.rawValue)&name=\(fileNamePhoto)&resolution=FHD&lang=" + (AppGlobalManager.sharedManager.loggedUser?.getUserLanguageUrl())!)!
此String
不是URL
。使用guard let/ if let
解包:
guard let languageUrl = AppGlobalManager.sharedManager.loggedUser?.getUserLanguageUrl(), let urlString = "\(ApiConstans.fullPath)?action=\(FileFolders.GET_TIPS_SLIDES.rawValue)&name=\(fileNamePhoto)&resolution=FHD&lang=\(languageUrl)".addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed), let productImageUrl = URL(string: urlString) else {
print("Problem in converting string to URL")
return
}
print(productImageUrl)
简单来说,您的String
应该在"http://" or "https://"
开头将String
转换为URL
。对于文档目录中的某些内容,您可以添加"file://"
。