public override void DrawInContext(CGContext context) { base.DrawInContext(context); nfloat size = NMath.Min(Bounds.Size.Width, Bounds.Size.Height); CGAffineTransform transform = AffineTransform; nfloat xTranslate = 0; nfloat yTranslate = 0; if (Bounds.Size.Width < Bounds.Size.Height) { yTranslate = (Bounds.Size.Height - size) / 2f; } else { xTranslate = (Bounds.Size.Width - size) / 2f; } transform.Translate(xTranslate, yTranslate); nfloat strokeWidth = StrokeFactor * size; nfloat checkBoxInset = InsetFactor * size; // Create the outer border for the check box. nfloat outerDimension = size - 2 * checkBoxInset; var checkBoxRect = new CGRect(checkBoxInset, checkBoxInset, outerDimension, outerDimension); checkBoxRect = transform.TransformRect(checkBoxRect); // Make the desired width of the outer box. context.SetLineWidth(strokeWidth); // Set the tint color of the outer box. context.SetStrokeColor(TintColor); // Draw the outer box. context.StrokeRect(checkBoxRect); // Draw the inner box if it's checked. if (Checked) { nfloat markInset = MarkInsetFactor * size; nfloat markDimension = size - 2 * markInset; var markRect = new CGRect(markInset, markInset, markDimension, markDimension); markRect = transform.TransformRect(markRect); context.SetFillColor(TintColor); context.FillRect(markRect); } }
void RenderLinearGradient(CGContext graphics, CGRect pathBounds, LinearGradientBrush brush) { using (CGColorSpace rgb = CGColorSpace.CreateDeviceRGB()) { CGColor[] colors = new CGColor[brush.GradientStops.Count]; nfloat[] locations = new nfloat[brush.GradientStops.Count]; for (int index = 0; index < brush.GradientStops.Count; index++) { Color color = brush.GradientStops[index].Color; colors[index] = new CGColor(new nfloat(color.R), new nfloat(color.G), new nfloat(color.B), new nfloat(color.A)); locations[index] = new nfloat(brush.GradientStops[index].Offset); } CGGradient gradient = new CGGradient(rgb, colors, locations); if (brush.Transform != null) { CGAffineTransform transform = (CGAffineTransform)brush.Transform.GetNativeObject(); // TODO: This does not adequately work. // There is no real SpreadMethod in iOS. pathBounds = transform.TransformRect(pathBounds); } graphics.DrawLinearGradient(gradient, new CGPoint(pathBounds.Left + brush.StartPoint.X * pathBounds.Width, pathBounds.Top + brush.StartPoint.Y * pathBounds.Height), new CGPoint(pathBounds.Left + brush.EndPoint.X * pathBounds.Width, pathBounds.Top + brush.EndPoint.Y * pathBounds.Height), CGGradientDrawingOptions.DrawsBeforeStartLocation | CGGradientDrawingOptions.DrawsAfterEndLocation); } }
public void SetClip(RectangleF rectangle) { RewindTransform(); RewindClip(); clipPath = null; clipBounds = currentTransform.TransformRect(rectangle.ToNS()); ApplyClip(); ApplyTransform(); }
public void TransformRect() { var transform = new CGAffineTransform(1, 2, 3, 4, 5, 6); var rect = transform.TransformRect(new CGRect(4, 5, 6, 7)); Assert.AreEqual((nfloat)24, rect.X, "X"); Assert.AreEqual((nfloat)34, rect.Y, "Y"); Assert.AreEqual((nfloat)27, rect.Width, "Width"); Assert.AreEqual((nfloat)40, rect.Height, "Height"); }
public object GetImageFromUri(FileUri imageUri, int targetWidth, int targetHeight, int rotation) { using (Stream stream = GetFileSystem().OpenFile(imageUri, UniversalFileMode.Open, UniversalFileAccess.Read, UniversalFileShare.Read)) { CGImageSource source = CGImageSource.FromUrl(imageUri.ToNSUrl()); CGImageOptions options = new CGImageOptions(); options.ShouldCache = false; var props = source.CopyProperties(options, 0); int imageWidth = ((NSNumber)props["PixelWidth"]).Int32Value; int imageHeight = ((NSNumber)props["PixelHeight"]).Int32Value; int scale = 1; while (imageWidth / scale / 2 >= (nfloat)targetWidth && imageHeight / scale / 2 >= (nfloat)targetHeight) { scale *= 2; } stream.Seek(0, SeekOrigin.Begin); UIImage image = UIImage.LoadFromData(NSData.FromUrl(imageUri.ToNSUrl()), scale); if (rotation != 0) { float radians = rotation * (float)Math.PI / 180f; CGRect imageFrame = new CGRect(0, 0, image.Size.Width, image.Size.Height); CGAffineTransform t = CGAffineTransform.MakeRotation(radians); imageFrame = t.TransformRect(imageFrame); CGSize rotatedSize = new CGSize(imageFrame.Width, imageFrame.Height); UIGraphics.BeginImageContext(rotatedSize); using (CGContext context = UIGraphics.GetCurrentContext()) { context.TranslateCTM(rotatedSize.Width / 2, rotatedSize.Height / 2); context.RotateCTM(-radians); context.ScaleCTM(1.0f, -1.0f); image.Draw(new CGRect(-image.Size.Width / 2, -image.Size.Height / 2, image.Size.Width, image.Size.Height)); image = UIGraphics.GetImageFromCurrentImageContext(); } UIGraphics.EndImageContext(); } return(image); } }
CGAffineTransform GetImageTransformAboutCenter(float angleDegrees, CGSize imageSize) { // Create a tranform that will rotate our image about its center CGAffineTransform transform = CGAffineTransform.MakeIdentity( ); // setup our transform. Translate it by the image's half width/height so it rotates about its center. transform.Translate(-imageSize.Width / 2, -imageSize.Height / 2); transform.Rotate(angleDegrees * Rock.Mobile.Math.Util.DegToRad); // now we need to concat on a post-transform that will put the image's pivot back at the top left. // get the image's dimensions transformed CGRect transformedImageRect = transform.TransformRect(new CGRect(0, 0, imageSize.Width, imageSize.Height)); // our post transform simply translates the image back CGAffineTransform postTransform = CGAffineTransform.MakeIdentity( ); postTransform.Translate(transformedImageRect.Width / 2, transformedImageRect.Height / 2); // now multiply the transform and postTranform and we have our final transform to use return(CGAffineTransform.Multiply(transform, postTransform)); }