美文网首页
本地文件存储

本地文件存储

作者: lalala1112389 | 来源:发表于2017-10-22 18:22 被阅读0次
拿到本地documents路径
 let docPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
//在此路径下创建文件夹
let filePath = "\(docPath!)/image1"
//为了防止重复建立,首先判断是否存在此文件,如果不存在则创建
 if (!fileManager.fileExists(atPath: filePath)){
                try?  fileManager.createDirectory(atPath: filePath, withIntermediateDirectories: true, attributes: nil)
    }
在文件夹下创建文件
    let uuid = UUID.init()
  //得到不同的文件名
   let imageNamePath = "\(filePath)/\(uuid).png"
//创建文件的路径以及文件名
try? fileManager.createFile(atPath: imageNamePath, contents: 
self?.imageData, attributes: nil)
 let pdfModel = PdfModel()
            pdfModel.imgPath = imageNamePath
            pdfModel.imgName = tempImg
           //将从相册中选择的图片添加到图片数组中去
            self?.pictureArray.append(pdfModel)
            DispatchQueue.main.async {
                self?.collectionView.reloadData()
           }

相关文章

网友评论

      本文标题:本地文件存储

      本文链接:https://www.haomeiwen.com/subject/sosvuxtx.html