/// <summary> /// Resize image maintain aspect ratio /// </summary> /// <param name="imageSource"></param> /// <param name="scale"></param> /// <returns></returns> public static UIImage ResizeImageWithAspectRatio(this UIImage imageSource, float scale) { if (scale > 1.0f) { return(imageSource); } using (var c = CIContext.Create()) { var sourceImage = CIImage.FromCGImage(imageSource.CGImage); var orientation = imageSource.Orientation; imageSource?.Dispose(); var transform = new CILanczosScaleTransform { Scale = scale, Image = sourceImage, AspectRatio = 1.0f }; var output = transform.OutputImage; using (var cgi = c.CreateCGImage(output, output.Extent)) { transform?.Dispose(); output?.Dispose(); sourceImage?.Dispose(); return(UIImage.FromImage(cgi, 1.0f, orientation)); } } }
/// <summary> /// Resize image maintain aspect ratio /// </summary> /// <param name="imageSource"></param> /// <param name="scale"></param> /// <returns></returns> public static UIImage ResizeImageWithAspectRatio(this UIImage imageSource, float scale) { if (scale > 1.0f) { return(imageSource); } using (var c = CIContext.Create()) { var sourceImage = CIImage.FromCGImage(imageSource.CGImage); var orientation = imageSource.Orientation; imageSource?.Dispose(); CILanczosScaleTransform transform = null; /*if(UIDevice.CurrentDevice.CheckSystemVersion(13, 0)) * { * transform = new CILanczosScaleTransform * { * Scale = scale, * InputImage = sourceImage, * AspectRatio = 1.0f * }; * } * else*/ //{ transform = new CILanczosScaleTransform { Scale = scale, Image = sourceImage, AspectRatio = 1.0f }; //} var output = transform.OutputImage; using (var cgi = c.CreateCGImage(output, output.Extent)) { transform?.Dispose(); output?.Dispose(); sourceImage?.Dispose(); return(UIImage.FromImage(cgi, 1.0f, orientation)); } } }