【发布时间】: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]()。因为我认为你在某处有一个 varmyImages。你如何删除那条线。会发生什么? -
myImages.append(image) 的行我收到一条错误消息,提示使用未解析的标识符 myImages。
-
你在哪里初始化了
tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)中的 varmyImages?
标签: ios swift uitableview uiimageview