2015-08-13 58 views
1

我正在开发一个应用程序,其中用户可以保存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) 

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

回答

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) 
} 

希望它有帮助!

+0

谢谢Jiri!我更改了 'let documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.CachesDirectory,.UserDomainMask,true)[0] as!字符串' 至 'let documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0] as!字符串' ,它的工作。 –

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) 

,或者你可以删除旧文件,写上新的关于文件管理器访问的详细信息here

+0

感谢您的回答。你能把这个与我写的代码结合起来吗?我对swift很陌生,对于参数和所有问题我都有问题! –

+0

更新了我的代码 –

+0

它给出了一个错误“找不到使用UsingNewMetadataOnly的成员” –

相关问题