将图像写入目录中

时间:2022-02-27 21:22:28

I am getting an error when writing an image file to a directory in Xcode. The function data.writeToFile is returning an error. Here is what I am trying to do:

在Xcode中将映像文件写入目录时会出现错误。函数数据。writeToFile返回一个错误。以下是我想做的:

Get The File Path:

获取文件路径:

func getPath(fileName: String) -> String {
        let documentURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
        let folder = "sampleDirectory"
        return documentURL.URLByAppendingPathComponent(folder).URLByAppendingPathComponent(fileName).path!
    }

Save the Image

保存图像

func saveImage(image: UIImage, path: String) -> Bool {
        let pngImageData = UIImagePNGRepresentation(image)
        do {
            let success = try pngImageData?.writeToFile(path, options: NSDataWritingOptions.init(rawValue: 0))
        } catch {
            print(error)
        }
        return false
    }

However, there is an error saying:

然而,有一个错误的说法是:

NSPOSIXErrorDomain - code : 2

Does anyone know what the problem could be?

有人知道问题出在哪里吗?

EDIT Where I call the code:

编辑我调用的代码:

let fileName = "first_image"
let imagePath = self.getPath(fileName)
let result = self.saveImage(processedImage, path: imagePath)

processedImage is of type UIImage!

processedImage是UIImage类型!

1 个解决方案

#1


1  

Try creating the directory "sampleDirectory" if it does not exists or don't use a subdirectory. You can check if the directory exists with:

如果不存在或不使用子目录,请尝试创建目录“sampleDirectory”。您可以检查目录是否存在:

if !NSFileManager.defaultManager().fileExistsAtPath(path) { // create missing directories try! NSFileManager.defaultManager().createDirectoryAtPath(foo, withIntermediateDirectories: true, attributes: nil) }

如果!NSFileManager.defaultManager().fileExistsAtPath(path){//创建丢失的目录,请尝试!NSFileManager.defaultManager().createDirectoryAtPath(foo, withintermediatedirectory: true, attributes: nil)}

You also might want to use the option NSDataWritingOptions.DataWritingAtomic which first write to an auxiliary file first and then exchange the files if there were no errors

您可能还想使用选项NSDataWritingOptions。DataWritingAtomic:首先写入辅助文件,然后在没有错误的情况下交换文件

#1


1  

Try creating the directory "sampleDirectory" if it does not exists or don't use a subdirectory. You can check if the directory exists with:

如果不存在或不使用子目录,请尝试创建目录“sampleDirectory”。您可以检查目录是否存在:

if !NSFileManager.defaultManager().fileExistsAtPath(path) { // create missing directories try! NSFileManager.defaultManager().createDirectoryAtPath(foo, withIntermediateDirectories: true, attributes: nil) }

如果!NSFileManager.defaultManager().fileExistsAtPath(path){//创建丢失的目录,请尝试!NSFileManager.defaultManager().createDirectoryAtPath(foo, withintermediatedirectory: true, attributes: nil)}

You also might want to use the option NSDataWritingOptions.DataWritingAtomic which first write to an auxiliary file first and then exchange the files if there were no errors

您可能还想使用选项NSDataWritingOptions。DataWritingAtomic:首先写入辅助文件,然后在没有错误的情况下交换文件