title: Animated Gif Frames To Array of BufferedImages
body: <p>I am trying to extract all the frames of an animated gif to an array of bufferedimages. I have been reading <a href="https://stackoverflow.com/questions/8933893/convert-each-animated-gif-frame-to-a-separate-bufferedimage">Convert each animated GIF frame to a separate BufferedImage</a> and it was fairly easy to write each frame to a seperate file. But my problem comes up when I try to fill an ArrayList with the frames instead of writing them. Every image in the ArrayList is just the last frame of the gif.</p>
  
  <p>To make it more clear, this code will write each frame to seperate files perfectly:</p>
  
  <pre><code>    ArrayList&lt;BufferedImage&gt; frames = new ArrayList&lt;BufferedImage&gt;();
      BufferedImage master = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
  
      ImageReader ir = new GIFImageReader(new GIFImageReaderSpi());
      ir.setInput(ImageIO.createImageInputStream(gif));
      for (int i = 0; i &lt; ir.getNumImages(true); i++)
      {
          master.getGraphics().drawImage(ir.read(i), 0, 0, null);
          ImageIO.write(master, "gif", new File(dirGifs + "/frames" + i + ".gif"));
      }
  </code></pre>
  
  <p>However, this code will only gives me an ArrayList full of the same frame (being the last frame of the gif)</p>
  
  <pre><code>    ArrayList&lt;BufferedImage&gt; frames = new ArrayList&lt;BufferedImage&gt;();
      BufferedImage master = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
  
      ImageReader ir = new GIFImageReader(new GIFImageReaderSpi());
      ir.setInput(ImageIO.createImageInputStream(gif));
      for (int i = 0; i &lt; ir.getNumImages(true); i++)
      {
          master.getGraphics().drawImage(ir.read(i), 0, 0, null);
          frames.add(master);
      }
  </code></pre>
  
  <p>I thought that it was because I wasnt disposing of the graphics afterwards, but I tried creating a graphics object and disposing it and nothing changed. Need help!</p>