1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- using System;
- using System.IO;
- using CoreGraphics;
- using Foundation;
- using UIKit;
- [assembly: Xamarin.Forms.Dependency(typeof(InABox.Mobile.iOS.ImageToolsiOS))]
- namespace InABox.Mobile.iOS
- {
-
- public class ImageToolsiOS : IImageTools
- {
-
- public byte[] CreateThumbnail(byte[] source, float maxwidth, float maxheight)
- {
- byte[] result = { };
- using (UIImage src = UIImage.LoadFromData(NSData.FromArray(source)))
- {
- if (src != null)
- {
- if ((src.Size.Width > 0.0F) && (src.Size.Height > 0.0F))
- {
- var maxFactor = Math.Min(maxwidth / src.Size.Width, maxheight / src.Size.Height);
- var width = maxFactor * src.Size.Width;
- var height = maxFactor * src.Size.Height;
- UIGraphics.BeginImageContextWithOptions(new CGSize((float)width, (float)height), true, 1.0f);
- src.Draw(new CGRect(0, 0, (float)width, (float)height));
- var tgt = UIGraphics.GetImageFromCurrentImageContext();
- UIGraphics.EndImageContext();
- using (NSData imageData = tgt.AsJPEG())
- {
- result = new byte[imageData.Length];
- System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, result, 0,
- Convert.ToInt32(imageData.Length));
- }
- }
- }
- }
- return result;
- }
- }
- }
|