我正在开发一个应用程序,用户可以保存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 )如何覆盖保存的图像? 当我使用代码保存具有相同文件名的不同屏幕截图的图像,然后检索它时,我得到的是之前保存的图像,并且新图像未被覆盖!
I am developing an app where user can save 13 screenshots and display them on a single view as thumbnails or as a full screen image. This is how I save the screenshots:
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()and this is how i retrieve them:
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 )How do i overwrite the saved images ? When i use the code to save imageof a different screenshot with same filename and then retrieve it i get the previous image that was initially saved and new image is not overwritten!
最满意答案
我让你的代码更清洁并解决了问题! 评论将帮助你学习一些关于它如何工作的基本知识。
首先,定义从文件名给出完整文件路径的函数:
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) }希望能帮助到你!
I made your code cleaner and fixed the problem! Comments will help you learn some basic stuff about how it works.
First, define function that gives you full filepath from filename:
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) }And then, function that takes UIImage and saves it, or replaces it, depending:
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() }Also, following the same style, you can get image:
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) }Hope it helps!
更多推荐
发布评论