Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extremely BASIC iOS Gif support. (Have to start somewhere) #470

Merged
merged 1 commit into from
May 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@
<Compile Include="Pages\StreamListPageCell.xaml.cs">
<DependentUpon>StreamListPageCell.xaml</DependentUpon>
</Compile>
<Compile Include="Pages\SimpleGifPageModel.cs" />
<Compile Include="Pages\SimpleGifPage.xaml.cs">
<DependentUpon>SimpleGifPage.xaml</DependentUpon>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Import Project="..\..\..\packages\Xamarin.Forms.2.0.1.6505\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets" Condition="Exists('..\..\..\packages\Xamarin.Forms.2.0.1.6505\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets')" />
Expand Down Expand Up @@ -278,6 +282,9 @@
<EmbeddedResource Include="Pages\StreamListPageCell.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="Pages\SimpleGifPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Folder Include="Pages\" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public MenuPageModel()
})
},

new MenuItem() {
Section = "Basic",
Title = "Simple Gif",
Command = new BaseCommand(async (param) =>
{
await this.PushPageFromCacheAsync<SimpleGifPageModel>(pm => pm.Reload());
})
},

new MenuItem() {
Section = "Basic",
Title = "Placeholders examples",
Expand Down
12 changes: 12 additions & 0 deletions samples/ImageLoading.Forms.Sample/Shared/Pages/SimpleGifPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="FFImageLoading.Forms.Sample.SimpleGifPage"
xmlns:xamvvm="clr-namespace:Xamvvm;assembly=Xamvvm.Forms"
xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
xmlns:fftransformations="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Transformations"
Title="Simple Gif">
<ContentPage.Content>
<ffimageloading:CachedImage WidthRequest="300" HeightRequest="300"
DownsampleToViewSize="true" Source="{Binding ImageUrl}">
</ffimageloading:CachedImage>
</ContentPage.Content>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Xamarin.Forms;
using Xamvvm;

namespace FFImageLoading.Forms.Sample
{
public partial class SimpleGifPage : ContentPage, IBasePage<SimpleGifPageModel>
{
public SimpleGifPage()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Xamvvm;

namespace FFImageLoading.Forms.Sample
{
[PropertyChanged.ImplementPropertyChanged]
public class SimpleGifPageModel : BasePageModel
{
public void Reload()
{
ImageUrl = "https://media.giphy.com/media/l0Hlyi4ZMJI9MpFUQ/giphy.gif";
}

public string ImageUrl { get; set; }
}
}
1 change: 1 addition & 0 deletions source/FFImageLoading.Touch/FFImageLoading.Touch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="DataResolvers\FileDataResolver.cs" />
<Compile Include="DataResolvers\UrlDataResolver.cs" />
<Compile Include="DataResolvers\StreamDataResolver.cs" />
<Compile Include="Helpers\GifHelper.cs" />
</ItemGroup>
<Import Project="..\FFImageLoading.Cross\FFImageLoading.Cross.projitems" Label="Shared" Condition="Exists('..\FFImageLoading.Cross\FFImageLoading.Cross.projitems')" />
<Import Project="..\FFImageLoading.Shared\FFImageLoading.Shared.projitems" Label="Shared" Condition="Exists('..\FFImageLoading.Shared\FFImageLoading.Shared.projitems')" />
Expand Down
79 changes: 79 additions & 0 deletions source/FFImageLoading.Touch/Helpers/GifHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Collections.Generic;
using System.Linq;
using Foundation;
using ImageIO;
using UIKit;

namespace FFImageLoading.Helpers
{
public static class GifHelper
{
public static UIImage AnimateGif(NSData data)
{
if (data?.Length == 0)
return null;

using (var source = CGImageSource.FromData(data))
return AnimateGifFromSource(source) ?? UIImage.LoadFromData(data);
}

private static UIImage AnimateGifFromSource(CGImageSource source)
{
var frameCount = source?.ImageCount;

// no need to animate
if (frameCount <= 1)
return null;

var frames = GetFrames(source);
var delays = GetDelays(source);
var totalDuration = delays.Sum();

// SUPER BASIC. Does not respect variable length frames. No memory optimizations.
return UIImage.CreateAnimatedImage(frames.ToArray(), totalDuration);
}

private static List<UIImage> GetFrames(CGImageSource source)
{
var retval = new List<UIImage>();

for (int i = 0; i < source?.ImageCount; i++)
{
using (var frameImage = source.CreateImage(i, null))
retval.Add(UIImage.FromImage(frameImage));
}

return retval;
}

private static List<double> GetDelays(CGImageSource source)
{
var retval = new List<double>();

for (int i = 0; i < source?.ImageCount; i++)
{
var properties = source.GetProperties(i, null);
using (var gifProperties = properties.Dictionary["{GIF}"])
{
using (var delayTime = gifProperties.ValueForKey(new NSString("DelayTime")))
{
var realDuration = double.Parse(delayTime.ToString());
retval.Add(realDuration);
}
}
}

return retval;
}

private static int GetLoopCount(CGImageSource source)
{
var var = source.GetProperties(null);
using (var gifProperties = var.Dictionary["{GIF}"])
{
var loopCount = gifProperties.ValueForKey(new NSString("LoopCount"));
return int.Parse(loopCount.ToString());
}
}
}
}
6 changes: 6 additions & 0 deletions source/FFImageLoading.Touch/Work/PlatformImageLoaderTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ protected async override Task<UIImage> GenerateImageAsync(string path, ImageSour
{
imageIn = new WebP.Touch.WebPCodec().Decode(imageData);
}
// Special case to handle gif animations on iOS
else if (path.ToLowerInvariant().EndsWith(".gif", StringComparison.InvariantCulture))
{
using (var nsdata = NSData.FromStream(imageData))
imageIn = GifHelper.AnimateGif(nsdata);
}
else
{
var nsdata = NSData.FromStream(imageData);
Expand Down