【问题标题】:Saving an uiimageview to tableview in swift?快速将uiimageview保存到tableview?
【发布时间】:2015-10-27 03:54:30
【问题描述】:

我不认为我这样做是正确的。我有这个绘图应用程序,一旦我在 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
}

【问题讨论】:

  • myImages 中的saveButton 是什么:var myImages: [UIImage] = [UIImage]()
  • 我应该将它保存到我的图像视图而不是像那样声明它吗?因为我有一个用于绘图的 UIImageview 的 @IBOutlet。
  • 我不知道你为什么要写这行var myImages: [UIImage] = [UIImage]()。因为我认为你在某处有一个 var myImages。你如何删除那条线。会发生什么?
  • myImages.append(image) 的行我收到一条错误消息,提示使用未解析的标识符 myImages。
  • 你在哪里初始化了 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) 中的 var myImages

标签: ios swift uitableview uiimageview


【解决方案1】:

在模型更新后,您需要在saveButton 中重新加载表格,类似于self.tableView.reloadData(),以便表格视图选择最新数据并呈现它。

作为旁注,您应该将它们的本地路径保存在数组中的文件系统中,并从您的 cellForRowAtIndexPath 函数中的路径加载它们,而不是将 UIImage 存储在数组中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    相关资源
    最近更新 更多