ImageToolsiOS.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.IO;
  3. using CoreGraphics;
  4. using Foundation;
  5. using UIKit;
  6. [assembly: Xamarin.Forms.Dependency(typeof(InABox.Mobile.iOS.ImageToolsiOS))]
  7. namespace InABox.Mobile.iOS
  8. {
  9. public class ImageToolsiOS : IImageTools
  10. {
  11. public byte[] CreateThumbnail(byte[] source, float maxwidth, float maxheight)
  12. {
  13. byte[] result = { };
  14. using (UIImage src = UIImage.LoadFromData(NSData.FromArray(source)))
  15. {
  16. if (src != null)
  17. {
  18. if ((src.Size.Width > 0.0F) && (src.Size.Height > 0.0F))
  19. {
  20. var maxFactor = Math.Min(maxwidth / src.Size.Width, maxheight / src.Size.Height);
  21. var width = maxFactor * src.Size.Width;
  22. var height = maxFactor * src.Size.Height;
  23. UIGraphics.BeginImageContextWithOptions(new CGSize((float)width, (float)height), true, 1.0f);
  24. src.Draw(new CGRect(0, 0, (float)width, (float)height));
  25. var tgt = UIGraphics.GetImageFromCurrentImageContext();
  26. UIGraphics.EndImageContext();
  27. using (NSData imageData = tgt.AsJPEG())
  28. {
  29. result = new byte[imageData.Length];
  30. System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, result, 0,
  31. Convert.ToInt32(imageData.Length));
  32. }
  33. }
  34. }
  35. }
  36. return result;
  37. }
  38. }
  39. }