2016-09-18 75 views
6

我想將圖像放入使用GLKView繪製圖像的UITableViewCell中。GLKView繪製時爲空

我有一個故事板中的原型單元格,其中有一個GLKView。它沒有選中Enable setNeedsDisplay。

原型電池有所謂的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>>) 

我的問題是爲什麼它的視圖空白?我相信有一個圖像,glkView是在正確的地方(我已經檢查了Xcode中的預覽工具)

爲什麼繪製(rect :)被調用,而不是glkView(視圖:GLKView, drawRect:CGRect)?

我使用5運行iOS 9.3的iPhone的Xcode 8,快捷3,任何幫助非常感謝

+0

嘿馬克。你解決了嗎? –

+0

不,仍然無法正常工作 –

回答

1

我不知道,如果Xcode中會自動處理莫名其妙或上下文是否默認激活,但在我只有OpenGL的項目我做了,到目前爲止,我不得不使用:

[EAGLContext setCurrentContext:ciContext]; 

每當我需要我的情況下被激活。