我的保存按钮有这个代码,但现在我需要显示保存在tableview中的图形。我已经完成了表视图控制器的某些部分,但是为了显示图像并将其保存在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
}
答案 0 :(得分:0)
目前还不清楚你的应用程序是如何构建的,但基本思想仍然是相同的。
创建一个UIImage
数组,并在每次保存图像时添加到数组中。
创建数组 var myImages: [UIImage] = [UIIMage]()
(把这个放在UIImageView
和UITableView
可以访问它的地方。
将图像添加到数组: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)
}