0

我在 API 调用中获取 base64 图像,我需要将这些图像存储为本地文件。

我试图编写这些数据,而不必将字符串转换为 UIImage然后将UIImage 转换为JPEGRepresentation

如果可以避免的话,我不希望先创建 UIImages 然后更改为 JPEGs 的开销,但我不知道这是否可能?

我可以在我的应用程序容器中看到所有文件都在那里并且文件大小正确,但是当我尝试打开它们时,它们不会,并且我被告知它们可能已损坏。

extension NSData {

    func writeToURL2(named:URL, completion: @escaping (_ result: Bool, _ url:NSURL?) -> Void)  {

        let tmpURL = named as NSURL

        DispatchQueue.global(qos: .background).async { [weak self] () -> Void in

            guard let strongSelf = self else { completion (false, tmpURL); return }

            strongSelf.write(to: tmpURL as URL, atomically: true)
            var error:NSError?
            if tmpURL.checkResourceIsReachableAndReturnError(&error) {
                print("We have it")
                completion(true, tmpURL)
            } else {
                print("We Don't have it\(error?.localizedDescription)")
                completion (false, tmpURL)
            }

        }

    }
}

它的用法如下:

for employee in syncReponse
            {
                autoreleasepool{
                    if let employeeJsonStr = employee["data"] as? String{


                        if let employeeDataDict = try? JSONSerializer.toDictionary(employeeJsonStr), let proPic = employeeDataDict["profilepicture"] as? String, proPic.removingWhitespaces() != "", let idStr = employeeDataDict["employeeId"] as? String, let proPicData = (proPic.data(using: .utf8)) {

                            let empPicDir = mainDir.appendingPathComponent(util_Constants.DIR_EMP_PICS)
                            let filename = empPicDir.appendingPathComponent(idStr+".jpg")

                            (proPicData as NSData).writeToURL2(named: filename, completion: { (result, url) -> Void in

                            })
                        }

                        let Emp = DATA_EP_employee(employeeJsonStr : employeeJsonStr)
                        dataList.append(Emp)
                        reqCount += 1
                    }
                }
            }
4

0 回答 0