如何覆盖/删除应用程序中保存的图像(How to overwrite/delete saved images in app)

系统教程 行业动态 更新时间:2024-06-14 17:03:54
如何覆盖/删除应用程序中保存的图像(How to overwrite/delete saved images in app)

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

更多推荐

本文发布于:2023-04-24 14:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/adb8940fc1eb758fe1c3cc257fc9b542.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   图像   overwrite   delete   images

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!