GLKView is blank when drawn
Asked Answered
S

1

7

I am trying to place images into a UITableViewCell using a GLKView to draw the image.

I have a prototype cell in a storyboard, in which I have a GLKView. It does not have Enable setNeedsDisplay checked.

The prototype cell has a custom class called PhotoTableViewCell which is

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")
  }
}

This is used in a 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
}

When I navigate to the TableView I get the following log statements

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>>)

My questions are why it the view blank? I am sure that there is an image, that the glkView is in the right place (I have inspected it with the preview tool in Xcode)

Why is draw(rect:) being called and not glkView( view:GLKView, drawIn rect:CGRect) ?

I am using Xcode 8, swift 3 on an iPhone 5 running iOS 9.3, any help greatly appreciated

Silvan answered 18/9, 2016 at 23:54 Comment(2)
Hey Mark. Did you solve it?Prisoner
No, still not workingSilvan
F
1

I am not sure if Xcode handles this automatically somehow or if the context is by default activated, but in my only OpenGL project I've made so far, I had to use:

[EAGLContext setCurrentContext:ciContext];

whenever I needed my context to be active.

Formulary answered 29/9, 2016 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.