简体   繁体   中英

How to make Generated gif in xamarin.ios slower?

I created a service used in my xamarin.forms project to generate a gif from four downloaded frame. The android side is working good, but I got a problem in iOs side, where gif is created but it's too fast, regardless of the set delay value. This is my class:

public class GifService : IGifService
{
    public string CreateGif(string frame1Path, string frame2Path, string frame3Path, string frame4Path,string webId,string path="")
    {
        List<UIImage> listOfFrame = new List<UIImage>();

        UIImage image1 = new UIImage(frame1Path);

        listOfFrame.Add(image1);

        UIImage image2 = new UIImage(frame2Path);

        listOfFrame.Add(image2);

        UIImage image3 = new UIImage(frame3Path);

        listOfFrame.Add(image3);

        UIImage image4 = new UIImage(frame4Path);

        listOfFrame.Add(image4);

        NSMutableDictionary fileProperties = new NSMutableDictionary();
        fileProperties.Add(CGImageProperties.GIFLoopCount, new NSNumber(0));

        NSMutableDictionary frameProperties = new NSMutableDictionary();
        frameProperties.Add(CGImageProperties.GIFDelayTime, new NSNumber(5f));

        NSUrl documentsDirectoryUrl = NSFileManager.DefaultManager.GetUrl(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User,null, true,out _);
        NSUrl fileUrl = documentsDirectoryUrl.Append(webId + ".gif",false);

        var destination = CGImageDestination.Create(fileUrl, MobileCoreServices.UTType.GIF, 4);
        destination.SetProperties(fileProperties);

        foreach(var frame in listOfFrame)
        {
            var cgImage = frame.CGImage;
            if(cgImage!= null)
            {
                destination.AddImage(cgImage, frameProperties);
            }
        }

        if (!destination.Close())
        {
            Console.WriteLine("Failed to finalize the image destination");
        }
        return fileUrl.Path;
    }
}

I think that the problem is CGImageProperties.GIFDelayTime that is ignored, but i don't know why. How can I resolve this problem?

After some tries, I found finally a solution to generate a gif with a desired delay. I don't know why, but in the way i showed in my question, the options are ignored. Here a working solution:

public class GifService : IGifService
{
    public string CreateGif(string frame1Path, string frame2Path, string frame3Path, string frame4Path,string webId,string path="")
    {
        List<UIImage> listOfFrame = new List<UIImage>();

        UIImage image1 = new UIImage(frame1Path);

        listOfFrame.Add(image1);

        UIImage image2 = new UIImage(frame2Path);

        listOfFrame.Add(image2);

        UIImage image3 = new UIImage(frame3Path);

        listOfFrame.Add(image3);

        UIImage image4 = new UIImage(frame4Path);

        listOfFrame.Add(image4);

        NSMutableDictionary fileProperties = new NSMutableDictionary
        {
            { CGImageProperties.GIFLoopCount, new NSNumber(1) }
        };

        NSUrl documentsDirectoryUrl = NSFileManager.DefaultManager.GetUrl(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User,null, true,out _);
        NSUrl fileUrl = documentsDirectoryUrl.Append(webId + ".gif",false);

        var destination = CGImageDestination.Create(fileUrl, MobileCoreServices.UTType.GIF, 4);
        destination.SetProperties(fileProperties);

        foreach (var frame in listOfFrame)
        {
            //difference is here, i create a var option and i set the 
            //GifDictionary
            var options = new CGImageDestinationOptions();
            options.GifDictionary = new NSMutableDictionary();
            options.GifDictionary[CGImageProperties.GIFDelayTime] = new NSNumber(1f);
            var cgImage = frame.CGImage;
            if(cgImage!= null)
            {
                destination.AddImage(cgImage, options);
            }
        }

        if (!destination.Close())
        {
            Console.WriteLine("Failed to finalize the image destination");
        }
        return fileUrl.Path;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM