GWT图像来自服务器

时间:2016-12-02 10:33:53

标签: gwt

当我实施这里给出但不工作时 我的实施是 服务器端:

<ion-view title="{{content.title}}">
    ....

尽管客户端无法看到图像。

请清除我的怀疑 提前致谢

1 个答案:

答案 0 :(得分:0)

当您从服务器获取base64字符串时,您需要在DOM中的图像上添加一个加载处理程序。我这样做的方法是将它附加到DOM并隐藏它,如下面的代码所示。

/** get events preview from server, attach to the DOM and store in 'preview'
 * @param handler option to pass a custom load handler
 */
private void get_preview(LoadHandler handler) {
    final LoadHandler load_handler = handler;

    server.getPreviewImage(user, data, new AsyncCallback<String>() {

        @Override
        public void onFailure(Throwable caught) {
            log.info("getPreviewImage: " + error);
        }

        @Override
        public void onSuccess(String result) {
            preview = null;

            if (result != null) {
                ImageElement ie = doc.createImageElement();

                preview = Image.wrap(ie);
                preview.setVisible(false);

                doc.getElementById("imagedummy").removeAllChildren();
                doc.getElementById("imagedummy").appendChild(preview.getElement());

                // add load handler to DOM image before being able to use

                if (load_handler == null) {

                    preview.addLoadHandler(new LoadHandler() {
                        @Override
                        public void onLoad(LoadEvent event) {
                            display_preview();
                        }
                    });
                } else {
                    preview.addLoadHandler(load_handler);
                }

                preview.setUrl(result);
            }
        }
    });
}

/** Displays the preview on the canvas.
 * Resizes canvas if necessary and sets zoom
 */
private void display_preview() {
    EventSize size = data.getEventSize();

    canvas.canvas.setCoordinateSpaceWidth(size.width);
    canvas.canvas.setCoordinateSpaceHeight(size.height);

    float zoom = Float.parseFloat(preview_zoom.getSelectedValue());
    canvas.canvas.setPixelSize((int)(size.width * zoom), (int)(size.height * zoom));

    if (preview != null) {
        ImageElement elem = ImageElement.as(preview.getElement());
        canvas.canvas.getContext2d().drawImage(elem, 0, 0);
    }
}