-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IImageSourceHandler for iOS / Mac / Android
- Loading branch information
1 parent
ddf2ad5
commit 6566737
Showing
19 changed files
with
282 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
source/FFImageLoading.Forms.Droid/FFImageLoadingImageSourceHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Android.Content; | ||
using Android.Graphics; | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.Platform.Android; | ||
using FFImageLoading.Forms.Handlers; | ||
using FFImageLoading.Work; | ||
using FFImageLoading.Targets; | ||
|
||
namespace FFImageLoading.Forms.Platform | ||
{ | ||
public class FFImageLoadingImageSourceHandler : HandlerBase<Context>, IImageSourceHandler | ||
{ | ||
public async Task<Bitmap> LoadImageAsync(Xamarin.Forms.ImageSource imageSource, Context context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
if (!IsValid(context)) | ||
return null; | ||
|
||
var source = ImageSourceBinding.GetImageSourceBinding(imageSource, null); | ||
if (source == null) | ||
{ | ||
return null; | ||
} | ||
|
||
var result = await LoadImageAsync(source, imageSource, context, cancellationToken); | ||
var target = result.Target as BitmapTarget; | ||
return target?.BitmapDrawable?.Bitmap; | ||
} | ||
catch (Exception) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
private static bool IsValid(Context context) | ||
{ | ||
if (context == null || context.Handle == IntPtr.Zero) | ||
return false; | ||
|
||
#pragma warning disable CS0618 // Type or member is obsolete | ||
var activity = context as Android.App.Activity ?? (Android.App.Activity)Xamarin.Forms.Forms.Context; | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
if (activity != null) | ||
{ | ||
if (activity.IsFinishing) | ||
return false; | ||
if (activity.IsDestroyed) | ||
return false; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected override IImageLoaderTask GetImageLoaderTask(TaskParameter parameters, Context imageView) | ||
{ | ||
var target = new BitmapTarget(); | ||
var task = ImageService.CreateTask(parameters, target); | ||
ImageService.Instance.LoadImage(task); | ||
return task; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
source/FFImageLoading.Forms.Touch/FFImageLoadingImageSourceHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FFImageLoading.Forms.Handlers; | ||
using FFImageLoading.Work; | ||
|
||
#if __IOS__ | ||
using PImage = UIKit.UIImage; | ||
using PImageTarget = FFImageLoading.Targets.UIImageTarget; | ||
using Xamarin.Forms.Platform.iOS; | ||
#elif __MACOS__ | ||
using PImage = AppKit.NSImage; | ||
using PImageTarget = FFImageLoading.Targets.NSImageTarget; | ||
using Xamarin.Forms.Platform.MacOS; | ||
#endif | ||
|
||
namespace FFImageLoading.Forms.Platform | ||
{ | ||
public class FFImageLoadingImageSourceHandler : HandlerBase<object>, IImageSourceHandler | ||
{ | ||
public async Task<PImage> LoadImageAsync(Xamarin.Forms.ImageSource imageSource, CancellationToken cancellationToken = default, float scale = 1) | ||
{ | ||
try | ||
{ | ||
var source = ImageSourceBinding.GetImageSourceBinding(imageSource, null); | ||
if (source == null) | ||
{ | ||
return null; | ||
} | ||
|
||
var result = await LoadImageAsync(source, imageSource, null, cancellationToken); | ||
var target = result?.Target as PImageTarget; | ||
return target?.PImage; | ||
} | ||
catch (Exception) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
protected override IImageLoaderTask GetImageLoaderTask(TaskParameter parameters, object imageView) | ||
{ | ||
var target = new PImageTarget(); | ||
var task = ImageService.CreateTask(parameters, target); | ||
ImageService.Instance.LoadImage(task); | ||
return task; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,5 +44,6 @@ | |
</ItemGroup> | ||
<ItemGroup> | ||
<Folder Include="Helpers\" /> | ||
<Folder Include="Handlers\" /> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.