gpt4 book ai didi

c# - 如何使 xamarin.ios 中生成的 gif 变慢?

转载 作者:行者123 更新时间:2023-11-30 10:36:13 25 4
gpt4 key购买 nike

我创建了一个在我的 xamarin.forms 项目中使用的服务,用于从四个下载的帧生成 gif。 Android 端运行良好,但我在 iOs 端遇到问题,创建 gif 但速度太快,无论设置的延迟值如何。这是我的类(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;
}
}

我认为问题是被忽略的CGImageProperties.GIFDelayTime,但我不知道为什么。我该如何解决这个问题?

最佳答案

经过一番尝试,我终于找到了一个解决方案,可以生成具有所需延迟的 gif。我不知道为什么,但按照我在问题中显示的方式,选项被忽略。这是一个可行的解决方案:

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;
}
}

关于c# - 如何使 xamarin.ios 中生成的 gif 变慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57968598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com