Ios 如何在Swift中将图形保存到tableview?
我正在制作一个简单的绘图应用程序,当我按下“保存”按钮时,我正试图将我的绘图保存在tableview中。Im使用UIImageView允许用户绘制。这是使用NSUserdefaults保存图像的正确方法吗?如何将其保存在表视图中?非常感谢。这是我在“保存”按钮中输入的代码:Ios 如何在Swift中将图形保存到tableview?,ios,xcode,swift,uitableview,uiimageview,Ios,Xcode,Swift,Uitableview,Uiimageview,我正在制作一个简单的绘图应用程序,当我按下“保存”按钮时,我正试图将我的绘图保存在tableview中。Im使用UIImageView允许用户绘制。这是使用NSUserdefaults保存图像的正确方法吗?如何将其保存在表视图中?非常感谢。这是我在“保存”按钮中输入的代码: @IBAction func saveButton(sender: AnyObject) { self.navigationController?.popToRootViewControllerAnimated(true
@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()
}
您需要创建UITableViewController,并使用用户默认设置中的图像填充它 您需要将图像保存在数组中,并在UITableViewController中获取该数组。然后,在UITableViewDataSource函数中,您将返回一个由NSUserDefaults中相应图形填充的单元格 您需要实施:
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
您可能应该考虑将映像保存在核心数据或文件系统中。它更适合这种存储。虽然允许,但我们不应该使用
NSUserDefaults
来保存像图像这样的大量数据。您可以轻松地将文件系统用于此目的,如下所示:
保存图像:
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)
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent("myImage.png").path!
let image = UIImage(contentsOfFile: fileURL)
然后稍后获取图像并将其放在您的UITableView
获取图像:
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)
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent("myImage.png").path!
let image = UIImage(contentsOfFile: fileURL)
字符串myImage.png我可以在那里放任何东西对吗?因为只有在用户按下save时才会创建图像。正确-这是图像的名称!获取图像的代码我应该将其放在视图中吗?是否显示在tableview的MasterViewController中?这完全取决于您需要图像时的需要。在需要图像之前,我会延迟加载图像。如果保证显示图像,则可以将其加载到
viewdide
中。