public static void AssertEquals(this UIImage expectedBitmap, UIImage actualBitmap) { if (!actualBitmap.AsPNG().IsEqual(expectedBitmap.AsPNG())) { string failureMessage = null; expectedBitmap.InvokeOnMainThread(() => { var view = new UIView(); UIImageView actualView = new UIImageView() { Image = actualBitmap }; UIImageView expectedView = new UIImageView() { Image = expectedBitmap }; actualView.Frame = new CGRect(0, 0, actualBitmap.Size.Width, actualBitmap.Size.Height); expectedView.Frame = new CGRect(0, actualBitmap.Size.Height + 40, expectedBitmap.Size.Width, expectedBitmap.Size.Height); view.Frame = new CGRect(0, 0, actualView.Frame.Width + expectedView.Frame.Width, actualView.Frame.Height + expectedView.Frame.Height); view.AddSubviews(actualView, expectedView); failureMessage = CreateColorError(view.ToBitmap(), "Actual (top) vs Expected (bottom)"); }); Assert.Fail(failureMessage); } }
public static UIImage RoundedCorners(this UIImage image, float radius) { if (image == null) { throw new ArgumentNullException("image"); } UIImage converted = image; image.InvokeOnMainThread(() => { UIGraphics.BeginImageContext(image.Size); float imgWidth = image.Size.Width; float imgHeight = image.Size.Height; var c = UIGraphics.GetCurrentContext(); c.BeginPath(); c.MoveTo(imgWidth, imgHeight / 2); c.AddArcToPoint(imgWidth, imgHeight, imgWidth / 2, imgHeight, radius); c.AddArcToPoint(0, imgHeight, 0, imgHeight / 2, radius); c.AddArcToPoint(0, 0, imgWidth / 2, 0, radius); c.AddArcToPoint(imgWidth, 0, imgWidth, imgHeight / 2, radius); c.ClosePath(); c.Clip(); image.Draw(new PointF(0, 0)); converted = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); }); return(converted); }
public static UIImage Scale(UIImage image, float maxWidthAndHeight) { //Perform Image manipulation, make the image fit into a 48x48 tile without clipping. UIImage scaledImage = image; image.InvokeOnMainThread(() => { float fWidth = image.Size.Width; float fHeight = image.Size.Height; float fTotal = fWidth >= fHeight?fWidth:fHeight; float fDifPercent = maxWidthAndHeight / fTotal; float fNewWidth = fWidth * fDifPercent; float fNewHeight = fHeight * fDifPercent; SizeF newSize = new SizeF(fNewWidth, fNewHeight); UIGraphics.BeginImageContext(newSize); var context = UIGraphics.GetCurrentContext(); context.TranslateCTM(0, newSize.Height); context.ScaleCTM(1f, -1f); context.DrawImage(new RectangleF(0, 0, newSize.Width, newSize.Height), image.CGImage); scaledImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); }); return(scaledImage); }
public static UIImage Scale(UIImage image, float maxWidthAndHeight) { //Perform Image manipulation, make the image fit into a 48x48 tile without clipping. UIImage scaledImage = image; image.InvokeOnMainThread(() => { float fWidth = image.Size.Width; float fHeight = image.Size.Height; float fTotal = fWidth>=fHeight?fWidth:fHeight; float fDifPercent = maxWidthAndHeight / fTotal; float fNewWidth = fWidth*fDifPercent; float fNewHeight = fHeight*fDifPercent; SizeF newSize = new SizeF(fNewWidth,fNewHeight); UIGraphics.BeginImageContext (newSize); var context = UIGraphics.GetCurrentContext (); context.TranslateCTM (0, newSize.Height); context.ScaleCTM (1f, -1f); context.DrawImage (new RectangleF (0, 0, newSize.Width, newSize.Height), image.CGImage); scaledImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); }); return scaledImage; }
public static UIImage RoundCorners (UIImage image, int radius) { if (image == null) throw new ArgumentNullException ("image"); UIImage converted = image; image.InvokeOnMainThread(() => { UIGraphics.BeginImageContext (image.Size); float imgWidth = image.Size.Width; float imgHeight = image.Size.Height; var c = UIGraphics.GetCurrentContext (); c.BeginPath (); c.MoveTo (imgWidth, imgHeight/2); c.AddArcToPoint (imgWidth, imgHeight, imgWidth/2, imgHeight, radius); c.AddArcToPoint (0, imgHeight, 0, imgHeight/2, radius); c.AddArcToPoint (0, 0, imgWidth/2, 0, radius); c.AddArcToPoint (imgWidth, 0, imgWidth, imgHeight/2, radius); c.ClosePath (); c.Clip (); image.Draw (new PointF (0, 0)); converted = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); }); return converted; }
/// <summary> /// Creates a blurred image of an existing image at the given radius. /// </summary> /// <param name="originalImage">The original image.</param> /// <param name="radius">The radius.</param> /// <returns></returns> public static UIImage CreateBlurredImage(UIImage originalImage, int radius) { UIImage newImage = null; if (originalImage == null) { return(null); } originalImage.InvokeOnMainThread(() => newImage = InternalCreateBlurredImage(originalImage, radius)); return(newImage); }
/// <summary> /// Scales the image to th specified width, preserving aspect ratio. /// </summary> /// <returns>A new image at the requested width.</returns> /// <param name="image">The image to resize.</param> /// <param name="width">The target width.</param> public static UIImage ScaleImageToWidth(UIImage image, int width) { UIImage scaledImage = null; image.InvokeOnMainThread(() => { var size = new CGSize(width, width / image.Size.Width * image.Size.Height); UIGraphics.BeginImageContextWithOptions(size, false, UIScreen.MainScreen.Scale); var context = UIGraphics.GetCurrentContext(); context.TranslateCTM(0, size.Height); context.ScaleCTM(1.0f, -1.0f); context.DrawImage(new CGRect(CGPoint.Empty, size), image.CGImage); scaledImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); }); return(scaledImage); }