我觉得我这样做不对。我有这个绘图应用,一旦我在UIImageView中做了一个绘图,我想按下保存按钮并在表格视图中保存这个绘图的图像。我有这段代码但它不会在我按下保存时将绘图保存到表视图中。谁能告诉我我做错了什么,因为我想不出来。谢谢你!
//ViewController
@IBAction func saveButton(sender: AnyObject) {
var myImages: [UIImage] = [UIImage]()
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()
//Saving Image Here
myImages.append(image)
UIGraphicsEndImageContext()
let data = UIImagePNGRepresentation(image)
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent("image.png").path!
data?.writeToFile(fileURL, atomically: true)
}
//MasterViewController--------------------------------------------------
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myImages.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let imageView = UIImageView(frame: CGRectMake(0, 0, 40, 40))
imageView.image = myImages[indexPath.row]
cell.addSubview(imageView)
return cell
}
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
}
}
override func tableView(tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 40 //Some number to fit the image
}
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
在模型更新后,您需要重新加载saveButton
中的表,例如self.tableView.reloadData()
中的表视图,以选择最新的数据并呈现它。
作为一个旁注,而不是将UIImage
存储在数组中,您应该将它们的本地路径保存在数组的文件系统中,并从cellForRowAtIndexPath
函数的路径中加载它们。