C# 如何使xamarin.ios中生成的gif变慢?
我在我的xamarin.forms项目中创建了一个用于从四个下载的帧生成gif的服务。android端运行良好,但我在iOs端遇到了一个问题,在iOs端创建了gif,但无论设置的延迟值如何,它都太快了。这是我的班级:C# 如何使xamarin.ios中生成的gif变慢?,c#,swift,xamarin,xamarin.ios,gif,C#,Swift,Xamarin,Xamarin.ios,Gif,我在我的xamarin.forms项目中创建了一个用于从四个下载的帧生成gif的服务。android端运行良好,但我在iOs端遇到了一个问题,在iOs端创建了gif,但无论设置的延迟值如何,它都太快了。这是我的班级: public class GifService : IGifService { public string CreateGif(string frame1Path, string frame2Path, string frame3Path, string frame4Pat
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;
}
}
公共类GifService:IGifService
{
公共字符串CreateGif(字符串frame1Path、字符串frame2Path、字符串frame3Path、字符串frame4Path、字符串webId、字符串path=”“)
{
List LISTOFRAME=新列表();
UIImage image1=新UIImage(frame1Path);
添加(图1);
UIImage image2=新UIImage(frame2Path);
添加(图2);
UIImage image3=新UIImage(frame3Path);
添加(图3);
UIImage image4=新UIImage(frame4Path);
添加(图4);
NSMutableDictionary fileProperties=新的NSMutableDictionary();
添加(CGImageProperties.GIFLoopCount,新的NSNumber(0));
NSMutableDictionary frameProperties=新的NSMutableDictionary();
添加(CGImageProperties.gifdaytime,新的NSNumber(5f));
NSUrl documentsDirectoryUrl=NSFileManager.DefaultManager.GetUrl(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomain.User,null,true,out 41;;
NSUrl fileUrl=documentsDirectoryUrl.Append(webId+“.gif”,false);
var destination=cgimagestination.Create(fileUrl,MobileCoreServices.UTType.GIF,4);
destination.SetProperties(fileProperties);
foreach(listOfFrame中的var帧)
{
var cgImage=frame.cgImage;
if(cgImage!=null)
{
destination.AddImage(cgImage,frameProperties);
}
}
如果(!destination.Close())
{
Console.WriteLine(“未能完成映像目标”);
}
返回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;
}
}
公共类GifService:IGifService
{
公共字符串CreateGif(字符串frame1Path、字符串frame2Path、字符串frame3Path、字符串frame4Path、字符串webId、字符串path=”“)
{
List LISTOFRAME=新列表();
UIImage image1=新UIImage(frame1Path);
添加(图1);
UIImage image2=新UIImage(frame2Path);
添加(图2);
UIImage image3=新UIImage(frame3Path);
添加(图3);
UIImage image4=新UIImage(frame4Path);
添加(图4);
NSMutableDictionary fileProperties=新的NSMutableDictionary
{
{CGImageProperties.GIFLoopCount,新NSNumber(1)}
};
NSUrl documentsDirectoryUrl=NSFileManager.DefaultManager.GetUrl(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomain.User,null,true,out 41;;
NSUrl fileUrl=documentsDirectoryUrl.Append(webId+“.gif”,false);
var destination=cgimagestination.Create(fileUrl,MobileCoreServices.UTType.GIF,4);
destination.SetProperties(fileProperties);
foreach(listOfFrame中的var帧)
{
//不同之处在于,我创建了一个var选项,并设置
//礼品辞典
var options=新的CGImageDestinationOptions();
options.gifdirectionary=new NSMutableDictionary();
options.gifdedictionary[CGImageProperties.GIFDelayTime]=新的NSNumber(1f);
var cgImage=frame.cgImage;
if(cgImage!=null)
{
destination.AddImage(cgImage,选项);
}
}
如果(!destination.Close())
{
Console.WriteLine(“未能完成映像目标”);
}
返回fileUrl.Path;
}
}
将图像数组添加到animationImages属性中,并设置动画持续时间和计数什么是IGifService
?IGifService
是在共享项目级别创建的界面。我使用依赖注入来使用特定于平台的GifService
。我有两个GifService
,一个用于android项目,另一个用于这里编写的iOs项目。@Giuseppennisi Okey,尝试一下frameProperties.Add(CGImageProperties.GIFDelayTime,新的NSString(“0.5”)代码>@Junior Jiang-MSFT我试过了,但没用。