views:

209

answers:

1

I'm trying to display an NSTableView of:

| thumbnail | filename |

I created the NSTableView in IB and delegated it to a class. In the class, I bypassed a model just to get a POC implementation and created the data source delegate methods. They populate the text cells just fine. However, now I'm at the step where the first cell needs to contain a small thumbnail of the image.

What I want to do (and I'm sure it's stupid-simple) is grab the image icon -- and it's fair to assume that all files are jpegs and have thumbnails embedded -- and put that icon, scaled to 64x64 into the table cell. There's lots of code on how to generate thumbnails but I don't see much that gets me to working code. Here's what I have:

  # This works if I am only populating text values in the when 'Image'
  def tableView_objectValueForTableColumn_row_(image_table, column, row)
    thumbnailImage(75)
    case column.headerCell.stringValue
      when 'File Name'
        (0..99).to_a[row].to_s
      when 'Image'
        # here's where I want to return a square 64x64 image or ImageCell
        thumbnailImage(64)
      else
        '???'
    end
  end

  # Creates square thumbnail
  def thumbnailImage(size)
    file = "file://localhost/Users/sxross/Downloads/iStock_000004561564XSmall.jpg"
    image = CGImageSourceCreateWithURL(CFURLCreateWithString(nil, file, nil), nil)
    thumb = CGImageSourceCreateThumbnailAtIndex(image, 0, nil)
    thumb
  end

  def numberOfRowsInTableView(view)
    100
  end

What I'm grappling with is what the missing steps are to get the thumbnailImage method to provide me with something that can be an appropriate data object to return from the data source.

Any help is amazingly appreciated.

BTW: IknowIknowIknow, I should be using MacRuby but it doesn't run on my 32-bit Core Duo. Sadly.

A: 

I bypassed a model …

Don't do that. In Cocoa, it's easier to do things with a model than without.

What I'm grappling with is what the missing steps are to get the thumbnailImage method to provide me with something that can be an appropriate data object to return from the data source.

CGImageSourceCreateThumbnailAtIndex returns a CGImage. You need tableView_objectValueForTableColumn_row_ to return an NSImage. Therefore, use NSImage's initWithCGImage_size_ method.

If you're running Leopard, that method isn't available, so you'll need to create an NSBitmapImageRep with the CGImage instead, and then create an NSImage of the correct size and add that representation to it.

Peter Hosey