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
Having Rails & Cocoa Play Together
// 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 (http://pierlis.com)
# 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],
CGColorSpaceCreateWithName(KCGColorSpaceGenericRGB),
@qtz)
end
# 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
end
# 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}
).rubyString
end
end
See more on : <http://pierlis.com/blog/2008/1/2/having-rails-cocoa-play-together>






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