我正在尝试使用GLKView将图像放置到UITableViewCell中以绘制图像。

我在 Storyboard 中有一个原型(prototype)单元,其中有一个GLKView。它没有选中“启用setNeedsDisplay”。

原型(prototype)单元具有一个名为PhotoTableViewCell的自定义类,该类是

import UIKit
import GLKit

class PhotoTableViewCell: UITableViewCell {

  var eaglContext:EAGLContext!
  var ciContext:CIContext!
  var myImage:CIImage!

  @IBOutlet weak var photoGlView: GLKView!
  @IBOutlet weak var timeLabel: UILabel!
  @IBOutlet weak var cameraLabel: UILabel!
  @IBOutlet weak var notesLabel: UILabel!

  override func layoutSubviews() {
     self.photoGlView.delegate = self
 }

}

 extension PhotoTableViewCell: GLKViewDelegate {
   override func draw(_ rect: CGRect) {
       let drawableRectSize = CGSize(width: 200, height: 200)// width:   photoGlView.drawableWidth, height: photoGlView.drawableHeight)

      let drawableRect = CGRect(origin: CGPoint.zero, size: drawableRectSize)
      print("rect ->\(drawableRect)")
      print("rect size -> \(drawableRectSize)")
      ciContext.draw(myImage, in: drawableRect, from: myImage.extent)
      print("drawing rect my image =\(photoGlView)")
  }

  func glkView(_ view: GLKView, drawIn rect: CGRect) {
      let drawableRectSize = CGSize(width: view.drawableWidth, height: view.drawableHeight)
      let drawableRect = CGRect(origin: CGPoint.zero, size: drawableRectSize)
      ciContext.draw(myImage, in: drawableRect, from: myImage.extent)
      print("drawing")
  }
}

在UITableViewController中使用
class PhotosTableViewController: UITableViewController {

  var cameraId:Int   = 1
  var photos:[Photo] = []
  var eaglContext:EAGLContext!
  var ciContext:CIContext!

override func viewDidLoad() {
    super.viewDidLoad()
    self.eaglContext = EAGLContext(api: .openGLES2)
    self.ciContext = CIContext(eaglContext: eaglContext!)

}

....

   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "photoCell") as! PhotoTableViewCell
      let photo = photos[indexPath.row]
      if let image = photo.getLightImage()  {
          cell.ciContext      = ciContext
          cell.eaglContext    = eaglContext
          cell.myImage        = image
          cell.photoGlView.display()
      }
      cell.cameraLabel.text = AppService.shared.getCameraName(id: photo.cameraId)
      cell.timeLabel.text   = "Taken: \(photo.uploaded?.niceTime() ?? "")"
      return cell
}

当我导航到TableView时,我得到以下日志语句
rect ->(0.0, 0.0, 200.0, 200.0)
rect size -> (200.0, 200.0)
drawing rect my image =Optional(<GLKView: 0x1560bed0; frame = (0 0; 191 253); clipsToBounds = YES; opaque = NO; autoresize = RM+BM; tintColor =  UIDeviceRGBColorSpace 0 0 0 1; layer = <CAEAGLLayer: 0x1560e530>>)
rect ->(0.0, 0.0, 200.0, 200.0)
rect size -> (200.0, 200.0)
drawing rect my image =Optional(<GLKView: 0x156044f0; frame = (0 0; 191 253); clipsToBounds = YES; opaque = NO; autoresize = RM+BM; tintColor = UIDeviceRGBColorSpace 0 0 0 1; layer = <CAEAGLLayer: 0x15604610>>)

我的问题是为什么它的 View 空白?我肯定有一张图片,glkView在正确的位置(我已经用Xcode中的预览工具检查过它)

为什么调用draw(rect :)而不调用glkView(view:GLKView,drawIn rect:CGRect)?

我在运行iOS 9.3的iPhone 5上使用Xcode 8,swift 3,非常感谢任何帮助

最佳答案

我不确定Xcode是否会以某种方式自动处理此问题,或者默认情况下是否激活了上下文,但是到目前为止,在我所做的唯一的OpenGL项目中,我不得不使用:

[EAGLContext setCurrentContext:ciContext];

每当我需要活跃我的环境时。

关于ios - 绘制时GLKView为空白,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39563709/

10-09 01:39