将照片添加到document目录下

    //获取图片路径
    let imgpath = Bundle.main.path(forResource: "1", ofType: "jpg")
    //根据图片路径读取出二进制流(Data)
    let data = try! Data(contentsOf: URL(fileURLWithPath: imgpath!))
   
    var imgPath1 = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    imgPath1 += "1.jpg"
    let fileM1 = FileManager()
    //判断文件是否存在
    if fileM1.fileExists(atPath: imgPath1)
    {
        //创建文件
        fileM1.createFile(atPath: imgPath1, contents: nil, attributes: nil)
    }
    //写入文件
    try! data.write(to: URL(fileURLWithPath: imgPath1))
    let imgData = try! Data(contentsOf:URL(fileURLWithPath: imgPath1))
    let image = UIImage(data: imgData)
    let imageView = UIImageView(image: image)
    self.view.addSubview(imageView)
    try! data.write(to: URL(fileURLWithPath: imgPath1))

你可能感兴趣的:(将照片添加到document目录下)