【问题标题】:How do I save a drawing in a tableview after a user draws something in a UIImageView?用户在 UIImageView 中绘制内容后,如何将绘图保存在 tableview 中?
【发布时间】:2015-10-26 17:32:04
【问题描述】:

我的保存按钮有这段代码,但现在我需要显示保存在 tableview 中的绘图。我已经完成了表格视图控制器的某些部分,但是要让图像显示并保存在表格视图中是我坚持的地方。我该怎么做?代码如下:

@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 documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent("image.png").path!
data?.writeToFile(fileURL, atomically: true)
}


//CODE EDITED 
//MasterViewControllerTableView

var myImages: [UIImage] = [UIImage]()

override func viewDidAppear(animated: Bool) {


    let image = UIGraphicsGetImageFromCurrentImageContext()
    myImages.append(image)
}




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 cell2 = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as UITableViewCell
    let imageView = UIImageView(frame: CGRectMake(0, 0, 40, 40))
    imageView.image = myImages[indexPath.row]
    cell2.addSubview(imageView)
    return cell2
}


// 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
}

【问题讨论】:

    标签: ios xcode swift uitableview uiimageview


    【解决方案1】:

    目前尚不清楚您的应用程序是如何构建的,但基本思想仍然相同。

    创建一个UIImage 的数组,并在每次保存图像时添加到数组中。

    创建数组 var myImages: [UIImage] = [UIIMage]() (把这个放在UIImageViewUITableView 可以访问它的地方)。

    将图像添加到数组myImages.append(image)

    然后将您的numberOfRowsInSection 修改为:

    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]
        cel.addSubview(imageView)
        return cell
    }
    

    修改heightForRowAtIndexPath 以适应图像:

    func tableView(tableView: UITableView,
        heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
            return 40 //Some number to fit the image
    }
    

    这只是你可以做些什么来实现你正在寻找的东西的准系统。

    编辑

    @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()
        //Save 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)
    }
    

    【讨论】:

    • 当我在 UIImageView 中绘制图像并按保存时,由于某种原因它不会保存到表格视图中。我没有收到任何错误或崩溃。
    • @coding22 你是如何访问图片的?
    • 让 fileURL = filesURL.URLByAppendingPathComponent("image.png").path!
    • 我认为我做错了那部分。在 UIImageView 上画一些东西并按保存之前,我没有图像。
    • @coding22 看上面的代码。如果您想在tableView 中访问它,将UIImage 附加到数组并在需要图像时访问数组会容易得多。 let image = UIGraphicsGetImageFromCurrentImageContext() 然后myImages.append(image)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    相关资源
    最近更新 更多