如何在应用程序中覆盖/删除已保存的图像

时间:2015-08-13 09:39:33

标签: ios xcode swift

我正在开发一个应用程序,用户可以在其中保存13个屏幕截图并以缩略图或全屏图像的形式显示在单个视图中。这是我保存屏幕截图的方式:

let fileName:String = self.stickerUsed + "saved" + ".png"
                    var arrayPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! NSString
                    var pngFileName = arrayPaths.stringByAppendingPathComponent(fileName)

                    UIImagePNGRepresentation(resizeImage(screenshot!, newSize: CGSizeMake(self.view.frame.width, self.view.frame.height))).writeToFile(pngFileName, atomically:true)
                    NSUserDefaults.standardUserDefaults().setObject(pngFileName, forKey: self.stickerUsed)
                    NSUserDefaults.standardUserDefaults().synchronize()

这就是我检索它们的方式:

 var defaultName:String = self.stickerUsed + "saved" + ".png"
                let path = NSSearchPathForDirectoriesInDomains(
                    .DocumentDirectory, .UserDomainMask, true)[0] as! NSString
                let fileName = NSUserDefaults.standardUserDefaults()
                    .stringForKey(stickerUsed) ?? defaultName
                let imagePath = path.stringByAppendingPathComponent(fileName)
                let image = UIImage(named: imagePath )

如何覆盖保存的图像?当我使用代码保存具有相同文件名的不同屏幕截图的图像然后检索它时,我得到最初保存的新图像,并且不会覆盖新图像!

2 个答案:

答案 0 :(得分:0)

尝试您可以使用

var error:NSError?
    var resultingURL:NSURL?
    let oldURL:NSURL = NSURL.fileURLWithPath("<old path>")!
    let newURL:NSURL = NSURL.fileURLWithPath("<new path>")!
    fileManager.replaceItemAtURL(oldURL,
        withItemAtURL: newURL,
        backupItemName: nil,
        options: .UsingNewMetadataOnly,
        resultingItemURL: &resultingURL,
        error: &error)

或者您可以删除旧文件并编写新文件,有关FileManager的详细信息,请访问here

答案 1 :(得分:0)

我让你的代码更清洁并修复了问题!评论将帮助您了解有关其工作原理的一些基本知识。

首先,定义从文件名中提供完整文件路径的函数:

func documentsPathWithFileName(fileName : String) -> String {

    // Get file path to document directory root
    let documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0] as! String
    return documentsDirectoryPath.stringByAppendingPathComponent(fileName)
}

然后,取决于:

获取UIImage并保存或替换它的函数
func saveOverrideImage(screenshot : UIImage, stickerName : String) {

    // Get file path poining to documents directory
    let filePath = documentsPathWithFileName(stickerName)

    // We could try if there is file in this path (.fileExistsAtPath())
    // BUT we can also just call delete function, because it checks by itself
    NSFileManager.defaultManager().removeItemAtPath(filePath, error: nil)

    // Resize image as you want
    let image : NSData = UIImagePNGRepresentation(resizeImage(screenshot!, newSize: CGSizeMake(self.view.frame.width, self.view.frame.height)))

    // Write new image
    image.writeToFile(filePath, atomically: true)

    // Save your stuff to
    NSUserDefaults.standardUserDefaults().setObject(stickerName, forKey: self.stickerUsed)
    NSUserDefaults.standardUserDefaults().synchronize()
}

此外,遵循相同的风格,您可以获得图像:

func imageForStickerName(stickerName : String) -> UIImage? {

    // Get file path poining to documents directory
    let filePath = documentsPathWithFileName(stickerName)

    // Get image from file on given local url
    return UIImage(contentsOfFile: filePath)
}

希望它有所帮助!