0

我测试使用两种方法在我的应用程序中加载超过 2m 大小的图片。

private void loadImages() {
        long currentTime=System.currentTimeMillis();
        MediaTracker mediaTracker = new MediaTracker(this);
        images = new HashMap<String, Image>();
        for (Iterator iterator = model.getAllImages().iterator(); iterator
                .hasNext(); mediaTracker.addImage(tempImage, 0)) {
            String filename = (String) iterator.next();
            try{
            tempImage=ImageIO.read(Game.class.getResourceAsStream(filename));
            }catch(Exception e){

        }
            //URL imageURL = getClass().getResource(filename);
        //  tempImage = getImage(getCodeBase(), imageURL.toString());
            images.put(filename, tempImage);
        }

        try {
            mediaTracker.waitForID(0);
        } catch (InterruptedException interruptedexception) {
        }
        System.err.println(System.currentTimeMillis()-currentTime);
    }

我的测试结果显示ImageIO.read()速度比 . 慢两倍getImage()。有人能解释一下为什么吗?等你回复。

4

1 回答 1

1

I would say that two functions work in two different ways.

ImageIO is guaranteed to return you an image that can be painted immediately, where as getImage will return you a reference to a possible image which may be loaded at some time in the future, hence the reason you see paint methods that need ImageObserver

This would explain much of the difference between BufferedImage and Image. BufferedImage allows you direct access to things like the pixel and color model data

于 2012-09-26T10:53:37.947 回答