【发布时间】: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