Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

actionscript 3 - Flex: Render an unrealized UIComponent to BitmapData?

What is the best way to render to a UIComponent which hasn't been added to the stage? (I'm using UIComponents as renderers for objects, and want to render new copies for image export, filtering, etc.)

Two strategies I've seen/used so far include realizing the component to ensure it calls all the lifecycle methods:

  1. Add the component to Application.application, render with BitmapData.draw(), remove component. This is similar to what I've seen done for printing unrealized components as well.

  2. Add the component to a pop up window, render with BitmapData.draw(), dismiss popup after rendering complete.

I believe both of these just rely on the UI not refreshing while the current thread/event is executing, though (1) could also rely on the component being realized out of view.

Is there a better way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What I've used in the past with much success is the following:

  1. Create a new instance of your component
  2. Add an event listener for FlexEvent.CREATION_COMPLETE
  3. Set visible=false on the component
  4. Add the component as a child of the main Application
  5. When the component is created, the event listener function will be invoked. The rest of the logic should be put in / invoked from your event listener function
  6. Remove the event listener you added in step #2.
  7. Use ImageSnapshot.captureImage() or captureBitmapData() to capture the visual representation of the component.
  8. Remove the component from the main Application
  9. Process the image data as you need to.

I've used this to snapshot various Flex charting components for use in PDF's generated on the server side. After getting the BitmapData I use the PNGEncoder or JPEGEncoder classes to compress the data, then encode it in Base64 before uploading to the server.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...