Exemplo n.º 1
0
        private void DrawClippedBitmap(bool roundTop, CGBitmapContext bit, bool roundBottom)
        {
            // Clipping path for the image, different on top, middle and bottom.
            if (roundBottom)
            {
                bit.AddArc(Rad, Rad, Rad, (float)Math.PI, (float)(3 * Math.PI / 2), false);
            }
            else
            {
                bit.MoveTo(0, Rad);
                bit.AddLineToPoint(0, 0);
            }
            bit.AddLineToPoint(Dimx, 0);
            bit.AddLineToPoint(Dimx, Dimy);

            if (roundTop)
            {
                bit.AddArc(Rad, Dimy - Rad, Rad, (float)(Math.PI / 2), (float)Math.PI, false);
                bit.AddLineToPoint(0, Rad);
            }
            else
            {
                bit.AddLineToPoint(0, Dimy);
            }
            bit.Clip();
            bit.DrawImage(_rect, _scaled.CGImage);
        }
Exemplo n.º 2
0
        public static UIImage MakeRoundCornerImage(UIImage img, int cornerWidth, int cornerHeight)
        {
            UIImage newImage = null;

            if (null != img)
            {
                var w = img.Size.Width;
                var h = img.Size.Height;

                CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
                CGContext    context    = new CGBitmapContext(null, (int)w, (int)h, 8, (int)(4 * w), colorSpace, CGImageAlphaInfo.PremultipliedFirst);

                context.BeginPath();
                var rect = new RectangleF(0, 0, img.Size.Width, img.Size.Height);
                AddRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
                context.ClosePath();
                context.Clip();

                var cgImage = img.CGImage;
                context.DrawImage(new RectangleF(0, 0, w, h), cgImage);
                cgImage.Dispose();

                CGImage imageMasked = ((CGBitmapContext)context).ToImage();
                context.Dispose();
                colorSpace.Dispose();
                newImage = new UIImage(imageMasked);
                imageMasked.Dispose();
            }

            return(newImage);
        }
Exemplo n.º 3
0
        public override UITableViewCell GetCell(UITableView tv)
        {
            var cell = tv.DequeueReusableCell(ikey);

            if (cell == null)
            {
                cell = new UITableViewCell(UITableViewCellStyle.Default, ikey);
            }

            if (scaled == null)
            {
                return(cell);
            }

            Section psection    = Parent as Section;
            bool    roundTop    = psection.Elements [0] == this;
            bool    roundBottom = psection.Elements [psection.Elements.Count - 1] == this;

            using (var cs = CGColorSpace.CreateDeviceRGB()){
                using (var bit = new CGBitmapContext(IntPtr.Zero, dimx, dimy, 8, 0, cs, CGImageAlphaInfo.PremultipliedFirst)){
                    if (roundBottom)
                    {
                        bit.AddArc(rad, rad, rad, (float)Math.PI, (float)(3 * Math.PI / 2), false);
                    }
                    else
                    {
                        bit.MoveTo(0, rad);
                        bit.AddLineToPoint(0, 0);
                    }
                    bit.AddLineToPoint(dimx, 0);
                    bit.AddLineToPoint(dimx, dimy);

                    if (roundTop)
                    {
                        bit.AddArc(rad, dimy - rad, rad, (float)(Math.PI / 2), (float)Math.PI, false);
                        bit.AddLineToPoint(0, rad);
                    }
                    else
                    {
                        bit.AddLineToPoint(0, dimy);
                    }
                    bit.Clip();
                    bit.DrawImage(rect, scaled.CGImage);

                    cell.ImageView.Image = UIImage.FromImage(bit.ToImage());
                }
            }
            return(cell);
        }
Exemplo n.º 4
0
        public override void InitializeCell(UITableView tableView)
        {
            if (scaled != null)
            {
                ISection psection    = Parent as ISection;
                bool     roundTop    = psection.Elements[0] == this;
                bool     roundBottom = psection.Elements[psection.Elements.Count - 1] == this;

                using (var cs = CGColorSpace.CreateDeviceRGB())
                {
                    using (var bit = new CGBitmapContext(IntPtr.Zero, dimx, dimy, 8, 0, cs, CGImageAlphaInfo.PremultipliedFirst))
                    {
                        // Clipping path for the image, different on top, middle and bottom.
                        if (roundBottom)
                        {
                            bit.AddArc(rad, rad, rad, (float)Math.PI, (float)(3 * Math.PI / 2), false);
                        }
                        else
                        {
                            bit.MoveTo(0, rad);
                            bit.AddLineToPoint(0, 0);
                        }
                        bit.AddLineToPoint(dimx, 0);
                        bit.AddLineToPoint(dimx, dimy);

                        if (roundTop)
                        {
                            bit.AddArc(rad, dimy - rad, rad, (float)(Math.PI / 2), (float)Math.PI, false);
                            bit.AddLineToPoint(0, rad);
                        }
                        else
                        {
                            bit.AddLineToPoint(0, dimy);
                        }
                        bit.Clip();
                        bit.DrawImage(rect, scaled.CGImage);

                        Cell.ImageView.Image = UIImage.FromImage(bit.ToImage());
                    }
                }
            }
        }
Exemplo n.º 5
0
		public override UITableViewCell GetCell (UITableView tv)
		{
			var cell = tv.DequeueReusableCell (ikey);
			if (cell == null){
				cell = new UITableViewCell (UITableViewCellStyle.Default, ikey);
			}
			
			if (scaled == null)
				return cell;
			
			Section psection = Parent as Section;
			bool roundTop = psection.Elements [0] == this;
			bool roundBottom = psection.Elements [psection.Elements.Count-1] == this;
			
			using (var cs = CGColorSpace.CreateDeviceRGB ()){
				using (var bit = new CGBitmapContext (IntPtr.Zero, dimx, dimy, 8, 0, cs, CGImageAlphaInfo.PremultipliedFirst)){
					// Clipping path for the image, different on top, middle and bottom.
					if (roundBottom){
						bit.AddArc (rad, rad, rad, (float) Math.PI, (float) (3*Math.PI/2), false);
					} else {
						bit.MoveTo (0, rad);
						bit.AddLineToPoint (0, 0);
					}
					bit.AddLineToPoint (dimx, 0);
					bit.AddLineToPoint (dimx, dimy);
					
					if (roundTop){
						bit.AddArc (rad, dimy-rad, rad, (float) (Math.PI/2), (float) Math.PI, false);
						bit.AddLineToPoint (0, rad);
					} else {
						bit.AddLineToPoint (0, dimy);
					}
					bit.Clip ();
					bit.DrawImage (rect, scaled.CGImage);
					
					cell.ImageView.Image = UIImage.FromImage (bit.ToImage ());
				}
			}			
			return cell;
		}
Exemplo n.º 6
0
		public override void InitializeCell(UITableView tableView)
		{
			if (_Scaled != null)
			{
				bool roundTop = Section.Elements[0] == this;
				bool roundBottom = Section.Elements[Section.Elements.Count - 1] == this;
	
				using (var cs = CGColorSpace.CreateDeviceRGB())
				{
					using (var bit = new CGBitmapContext(IntPtr.Zero, dimx, dimy, 8, 0, cs, CGImageAlphaInfo.PremultipliedFirst))
					{
						// Clipping path for the image, different on top, middle and bottom.
						if (roundBottom)
						{
							bit.AddArc(rad, rad, rad, (float)Math.PI, (float)(3 * Math.PI / 2), false);
	
						}
						else
						{
							bit.MoveTo(0, rad);
							bit.AddLineToPoint(0, 0);
						}
						bit.AddLineToPoint(dimx, 0);
						bit.AddLineToPoint(dimx, dimy);
	
						if (roundTop)
						{
							bit.AddArc(rad, dimy - rad, rad, (float)(Math.PI / 2), (float)Math.PI, false);
							bit.AddLineToPoint(0, rad);
	
						}
						else
						{
							bit.AddLineToPoint(0, dimy);
						}
						bit.Clip();
						bit.DrawImage(rect, _Scaled.CGImage);
	
						Cell.ImageView.Image = UIImage.FromImage(bit.ToImage());
					}
				}
			}
		}
Exemplo n.º 7
0
        public static NSImage ToRounded(NSImage source, nfloat rad, double cropWidthRatio, double cropHeightRatio, double borderSize, string borderHexColor)
        {
            double sourceWidth  = source.CGImage.Width;
            double sourceHeight = source.CGImage.Height;

            double desiredWidth  = sourceWidth;
            double desiredHeight = sourceHeight;

            double desiredRatio = cropWidthRatio / cropHeightRatio;
            double currentRatio = sourceWidth / sourceHeight;

            if (currentRatio > desiredRatio)
            {
                desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
            }
            else if (currentRatio < desiredRatio)
            {
                desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);
            }

            float cropX = (float)((sourceWidth - desiredWidth) / 2);
            float cropY = (float)((sourceHeight - desiredHeight) / 2);

            if (rad == 0)
            {
                rad = (nfloat)(Math.Min(desiredWidth, desiredHeight) / 2);
            }
            else
            {
                rad = (nfloat)(rad * (desiredWidth + desiredHeight) / 2 / 500);
            }

            var       colorSpace       = CGColorSpace.CreateDeviceRGB();
            const int bytesPerPixel    = 4;
            int       width            = (int)desiredWidth;
            int       height           = (int)desiredHeight;
            var       bytes            = new byte[width * height * bytesPerPixel];
            int       bytesPerRow      = bytesPerPixel * width;
            const int bitsPerComponent = 8;

            using (var context = new CGBitmapContext(bytes, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big))
            {
                var clippedRect = new CGRect(0d, 0d, desiredWidth, desiredHeight);

                context.BeginPath();

                using (var path = NSBezierPath.FromRoundedRect(clippedRect, rad, rad))
                {
                    context.AddPath(path.ToCGPath());
                    context.Clip();
                }

                var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight);
                context.DrawImage(drawRect, source.CGImage);

                if (borderSize > 0d)
                {
                    borderSize = (borderSize * (desiredWidth + desiredHeight) / 2d / 1000d);
                    var borderRect = new CGRect((0d + borderSize / 2d), (0d + borderSize / 2d),
                                                (desiredWidth - borderSize), (desiredHeight - borderSize));

                    context.BeginPath();

                    using (var path = NSBezierPath.FromRoundedRect(borderRect, rad, rad))
                    {
                        context.SetStrokeColor(borderHexColor.ToUIColor().CGColor);
                        context.SetLineWidth((nfloat)borderSize);
                        context.AddPath(path.ToCGPath());
                        context.StrokePath();
                    }
                }

                using (var output = context.ToImage())
                {
                    return(new NSImage(output, CGSize.Empty));
                }
            }
        }
        public static NSImage ToTransformedCorners(NSImage source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize,
                                                   CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio)
        {
            double sourceWidth  = source.CGImage.Width;
            double sourceHeight = source.CGImage.Height;

            double desiredWidth  = sourceWidth;
            double desiredHeight = sourceHeight;

            double desiredRatio = cropWidthRatio / cropHeightRatio;
            double currentRatio = sourceWidth / sourceHeight;

            if (currentRatio > desiredRatio)
            {
                desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
            }
            else if (currentRatio < desiredRatio)
            {
                desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);
            }

            topLeftCornerSize     = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100;
            topRightCornerSize    = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100;
            bottomLeftCornerSize  = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100;
            bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100;

            float cropX = (float)((sourceWidth - desiredWidth) / 2);
            float cropY = (float)((sourceHeight - desiredHeight) / 2);

            var       colorSpace       = CGColorSpace.CreateDeviceRGB();
            const int bytesPerPixel    = 4;
            int       width            = (int)desiredWidth;
            int       height           = (int)desiredHeight;
            var       bytes            = new byte[width * height * bytesPerPixel];
            int       bytesPerRow      = bytesPerPixel * width;
            const int bitsPerComponent = 8;

            using (var context = new CGBitmapContext(bytes, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big))
            {
                context.BeginPath();

                using (var path = new NSBezierPath())
                {
                    // TopLeft
                    if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut))
                    {
                        path.MoveTo(new CGPoint(0, topLeftCornerSize));
                        path.LineTo(new CGPoint(topLeftCornerSize, 0));
                    }
                    else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded))
                    {
                        path.MoveTo(new CGPoint(0, topLeftCornerSize));
                        path.QuadCurveToPoint(new CGPoint(topLeftCornerSize, 0), new CGPoint(0, 0));
                    }
                    else
                    {
                        path.MoveTo(new CGPoint(0, 0));
                    }

                    // TopRight
                    if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut))
                    {
                        path.LineTo(new CGPoint(desiredWidth - topRightCornerSize, 0));
                        path.LineTo(new CGPoint(desiredWidth, topRightCornerSize));
                    }
                    else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded))
                    {
                        path.LineTo(new CGPoint(desiredWidth - topRightCornerSize, 0));
                        path.QuadCurveToPoint(new CGPoint(desiredWidth, topRightCornerSize), new CGPoint(desiredWidth, 0));
                    }
                    else
                    {
                        path.LineTo(new CGPoint(desiredWidth, 0));
                    }

                    // BottomRight
                    if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut))
                    {
                        path.LineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize));
                        path.LineTo(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight));
                    }
                    else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded))
                    {
                        path.LineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize));
                        path.QuadCurveToPoint(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight), new CGPoint(desiredWidth, desiredHeight));
                    }
                    else
                    {
                        path.LineTo(new CGPoint(desiredWidth, desiredHeight));
                    }

                    // BottomLeft
                    if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut))
                    {
                        path.LineTo(new CGPoint(bottomLeftCornerSize, desiredHeight));
                        path.LineTo(new CGPoint(0, desiredHeight - bottomLeftCornerSize));
                    }
                    else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded))
                    {
                        path.LineTo(new CGPoint(bottomLeftCornerSize, desiredHeight));
                        path.QuadCurveToPoint(new CGPoint(0, desiredHeight - bottomLeftCornerSize), new CGPoint(0, desiredHeight));
                    }
                    else
                    {
                        path.LineTo(new CGPoint(0, desiredHeight));
                    }

                    path.ClosePath();
                    context.AddPath(path.ToCGPath());
                    context.Clip();
                }

                var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight);
                context.DrawImage(drawRect, source.CGImage);


                using (var output = context.ToImage())
                {
                    return(new NSImage(output, CGSize.Empty));
                }
            }
        }
Exemplo n.º 9
0
		public static UIImage MakeRoundCornerImage(UIImage img, int cornerWidth, int cornerHeight)
		{
			UIImage newImage = null;
			 
			if (null != img)
			{
				var w = img.Size.Width;
				var h = img.Size.Height;
				 
				CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
				CGContext context = new CGBitmapContext(null, (int)w, (int)h, 8, (int)(4 * w), colorSpace, CGImageAlphaInfo.PremultipliedFirst);
				
				context.BeginPath();
				var rect = new RectangleF(0, 0, img.Size.Width, img.Size.Height);
				AddRoundedRectToPath (context, rect, cornerWidth, cornerHeight);
				context.ClosePath();
				context.Clip();
				
				var cgImage = img.CGImage;
				context.DrawImage (new RectangleF(0, 0, w, h), cgImage);
				cgImage.Dispose();
				
				CGImage imageMasked = ((CGBitmapContext)context).ToImage();
				context.Dispose();
				colorSpace.Dispose();
				newImage = new UIImage(imageMasked);
				imageMasked.Dispose();
			}
			 
			return newImage;
		}
Exemplo n.º 10
0
        public static NSImage ToCropped(NSImage source, double zoomFactor, double xOffset, double yOffset, double cropWidthRatio, double cropHeightRatio)
        {
            double sourceWidth  = source.CGImage.Width;
            double sourceHeight = source.CGImage.Height;

            double desiredWidth  = sourceWidth;
            double desiredHeight = sourceHeight;

            double desiredRatio = cropWidthRatio / cropHeightRatio;
            double currentRatio = sourceWidth / sourceHeight;

            if (currentRatio > desiredRatio)
            {
                desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
            }
            else if (currentRatio < desiredRatio)
            {
                desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);
            }

            xOffset = xOffset * desiredWidth;
            yOffset = yOffset * desiredHeight;

            desiredWidth  = desiredWidth / zoomFactor;
            desiredHeight = desiredHeight / zoomFactor;

            float cropX = (float)(((sourceWidth - desiredWidth) / 2) + xOffset);
            float cropY = (float)(((sourceHeight - desiredHeight) / 2) + yOffset);

            if (cropX < 0)
            {
                cropX = 0;
            }

            if (cropY < 0)
            {
                cropY = 0;
            }

            if (cropX + desiredWidth > sourceWidth)
            {
                cropX = (float)(sourceWidth - desiredWidth);
            }

            if (cropY + desiredHeight > sourceHeight)
            {
                cropY = (float)(sourceHeight - desiredHeight);
            }


            var       colorSpace       = CGColorSpace.CreateDeviceRGB();
            const int bytesPerPixel    = 4;
            int       width            = (int)desiredWidth;
            int       height           = (int)desiredHeight;
            var       bytes            = new byte[width * height * bytesPerPixel];
            int       bytesPerRow      = bytesPerPixel * width;
            const int bitsPerComponent = 8;

            using (var context = new CGBitmapContext(bytes, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big))
            {
                var clippedRect = new CGRect(0, 0, desiredWidth, desiredHeight);
                context.BeginPath();

                using (var path = NSBezierPath.FromRect(clippedRect))
                {
                    context.AddPath(path.ToCGPath());
                    context.Clip();
                }

                var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight);
                context.DrawImage(drawRect, source.CGImage);

                using (var output = context.ToImage())
                {
                    return(new NSImage(output, CGSize.Empty));
                }
            }
        }