我不认为我这样做是正确的。我有这个绘图应用程序,一旦我在UIImageView中绘图,我想按下保存按钮并在表格视图中保存绘图的图像。我有这个代码但是当我按保存时它不会将绘图保存到tableview。有人能告诉我我做错了什么因为我无法弄明白。谢谢!
//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
}
答案 0 :(得分:1)
您需要在更新模型之后在saveButton
中重新加载表格,如表格视图self.tableView.reloadData()
,以便选择最新数据并进行渲染。
作为旁注,不要将UIImage
存储在数组中,而应将其本地路径保存在数组中的文件系统中,并从cellForRowAtIndexPath
函数中的路径加载它们。