简体   繁体   English

如何在Swift中将绘图保存到tableview?

[英]How do I save a drawing to a tableview in Swift?

Im making a simple drawing app and Im trying to get my drawing saved in a tableview when I press the save button. 我正在制作一个简单的绘图应用程序,当我按下保存按钮时,我试图将我的绘图保存在tableview中。 Im using an UIImageView to allow the users to draw on. 我使用UIImageView允许用户绘图。 Is this the correct way to save an image with NSUserdefaults and how would I save it in a table view? 这是使用NSUserdefaults保存图像的正确方法吗?如何将其保存在表格视图中? Thank you! 谢谢! This is the code Im putting in my save button: 这是我放入保存按钮的代码:

@IBAction func saveButton(sender: AnyObject) {


self.navigationController?.popToRootViewControllerAnimated(true)
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.toolbarHidden = false


UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()



let data = UIImagePNGRepresentation(image)
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(data, forKey: "drawing")
userDefaults.synchronize()



}    

Although allowed, we should not use use NSUserDefaults to save heavy data like images. 虽然允许,但我们不应该使用NSUserDefaults来保存像图像这样的大量数据。 You can easily use file system for that purpose like this: 您可以轻松地将文件系统用于此目的,如下所示:

Saving the image: 保存图片:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let data = UIImagePNGRepresentation(image)

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent("myImage.png").path!

data?.writeToFile(fileURL, atomically: true)

And then later on fetch the image and put it on your UITableView 然后再获取图像并将其放在UITableView

Fetching the image: 获取图像:

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent("myImage.png").path!
let image = UIImage(contentsOfFile: fileURL)

You need to create a UITableViewController and populate it with your images from user defaults. 您需要创建一个UITableViewController并使用来自用户默认值的图像填充它。

You would need to save the images in an array and fetch that array in the UITableViewController. 您需要将图像保存在数组中并在UITableViewController中获取该数组。 Then in the UITableViewDataSource functions you would return a cell populated with the corresponding drawing from NSUserDefaults. 然后在UITableViewDataSource函数中,您将返回一个填充了NSUserDefaults中相应图形的单元格。

You need to implement: 你需要实现:

public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

You probably should look into saving the image in core data or the file system instead. 您可能应该考虑将图像保存在核心数据或文件系统中。 It's more suited for this kind of storage. 它更适合这种存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM