Graphics Reference
In-Depth Information
Obtaining the Current Image in Code
Now that we have published the Image output in our composition, we can obtain
the image data for the current image as an
NSImage
object. In fact, we can obtain
the image data in several different formats by specifying the type in our call to
-valueForOutputKey:ofType
. In the example code we are simply going to set the
contents
field of our root layer to the current image. In that case, we want to obtain a
CGImageRef
instead of an
NSImage
. This is trivial because all we need to do is specify
CGImage
as the type to return to our
-valueForOutputKey
call. Listing 9-3 demonstrates
how we can obtain the current image as a
CGImageRef
and set the contents of the
root layer.
LISTING 9-3
Obtaining Image Data as CGImageRef
- (
IBAction
)getDotScreenImage:(
id
)sender;
{
CGImageRef
image = (
CGImageRef
)
[qcLayer valueForOutputKey:
@”dotscreen_image”
ofType:
@”CGImage”
];
[[[window
contentView
]
layer
]
setContents
:(
id
)
image
];
}
We request the
dotscreen_image
output from the composition with a type of
CGImage,
and we get back a
CGImageRef
object that we use to set the
contents
of the root window
content view layer.
The call to
-valueForOutputKey:ofType
can return an
NSImage
,
NSBitmapImageRep
,
CGImage
,
CIImage
,
CVPixelBuffer
,
CVOpenGLBuffer
, or
CVOpenGLTexture
. Simply substitute
for the name
CGImage
and change the type returned according to the image type you
want to receive instead.
Notice that the
-getDotScreenImage
call is an
IBAction
. In interface builder, you need to
add a button to the window and connect it to this action in the
AppDelegate
object.
Figure 9-4 shows what the example project looks like when run.
Figure 9-5 shows what the example project looks like when the Get Image button has
been pressed. It sets the
contents
field of the root layer to the current image from
Movie 2, which has had the dot screen filter applied to it.
You determine which inputs and outputs are available to your code in Quartz Composer.
You can develop complex visualizations and then easily control them programmatically.
Just keep in mind that you can access only inputs and outputs that are available in the
Root Patch. If you create a
macro patch
(a patch that encapsulates other patches), you
need to expose inputs and outputs at the Root Patch level, or you cannot access them in
your calls to
-setValue:forInputKey
and
-valueForOutputKey:ofType
.