Flex: Render an unrealized UIComponent to BitmapData?

后端 未结 4 1446
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 12:36

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 ne

4条回答
  •  萌比男神i
    2020-12-15 13:15

    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.

提交回复
热议问题