ImageWriteParam defaultImageWriteParam = imageWriter.getDefaultWriteParam();
IIOMetadata imageMetaData = imageWriter.getDefaultImageMetadata(
ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB), defaultImageWriteParam);
String metaFormatName = imageMetaData.getNativeMetadataFormatName();
Node root = imageMetaData.getAsTree(metaFormatName);
IIOMetadataNode graphicsControlExtensionNode = (IIOMetadataNode) getOrCreateChild(root, "GraphicControlExtension");
// http://java.sun.com/javase/6/docs/api/javax/imageio/metadata/doc-files/gif_metadata.html
graphicsControlExtensionNode.setAttribute("disposalMethod", "none");
graphicsControlExtensionNode.setAttribute("userInputFlag", "FALSE");
graphicsControlExtensionNode.setAttribute("transparentColorFlag", "FALSE");
graphicsControlExtensionNode.setAttribute("delayTime", Integer.toString(userResource.getFrameDelay() / 100));
graphicsControlExtensionNode.setAttribute("transparentColorIndex", "0");
Node applicationExtensionsNode = getOrCreateChild(root, "ApplicationExtensions");
IIOMetadataNode netscapeExtension = new IIOMetadataNode("ApplicationExtension");
netscapeExtension.setAttribute("applicationID", "NETSCAPE");
netscapeExtension.setAttribute("authenticationCode", "2.0");
byte numLoops = (byte) (userResource.isLooped() ? 0x0 : 0x1);
netscapeExtension.setUserObject(new byte[] { 0x1, numLoops, 0x0 });
applicationExtensionsNode.appendChild(netscapeExtension);
imageMetaData.setFromTree(metaFormatName, root);
imageWriter.prepareWriteSequence(null);