DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Having Rails & Cocoa Play Together

  • submit to reddit
        // description of your code here
Play a Quartz composition from within a Rails application.
# Render a Quartz Composition ".qtz" file to buffer, as jpeg data
# Written by PLR from Pierlis (
# Use at your own risk, reuse at will.

# Cocoa and the Quartz framework must have been included in config/environment.rb.

class Composition
  # creates a composition from a .qtz file whose path is qtz_path
  def initialize(qtz_path, width = 350, height = 100)
    @qtz = QCComposition.compositionWithFile(qtz_path)
    raise "Can't find a valid composition at “#{qtz_path}”." if @qtz.nil?
    @renderer = QCRenderer.alloc.initOffScreenWithSize_colorSpace_composition(
                        [width, height],
  # renders the composition at time, with parameters, and return the jpeg data.
  def render(time = 0, parameters = {})
    # Only pass in parameters expected by the Qtz composition
    input_keys = @qtz.inputKeys
    parameters.each do |key, value|
      @renderer.setValue_forInputKey(value, key) if input_keys.include? key
    # And render the composition
    rendering_ok = @renderer.renderAtTime_arguments(time, nil)
    raise "Unable to render Quartz Composition" unless rendering_ok == 1
    # now convert the result to jpeg
    bitmapRep = @renderer.createSnapshotImageOfType('NSBitmapImageRep')
    jpegimagedata = bitmapRep.representationUsingType_properties(NSJPEGFileType, 
      {NSImageCompressionFactor => 0.8}

See more on : <>    


Snippets Manager replied on Tue, 2009/03/03 - 6:29am

I love the idea of playing Quartz composition under sort of Rails application, which will work great in top of quartz counter tops web-page in caesarstone. Will I have to type "render Quartz Composition" at the renderAtTime?