Exemplo n.º 1
0
        public static UIImage FocusZoom(
            UIImage image,
            float zoomFactor,
            int focusX,
            int focusY,
            bool antiAliasing = true,
            CGInterpolationQuality interpolationQuality = CGInterpolationQuality.High,
            UIColor paddingColor = null)
        {
            if (paddingColor == null)
            {
                paddingColor = UIColor.Clear;
            }

            if (Math.Abs(zoomFactor - 0.0f) < Tools.Maths.EPSILON_F)
            {
                throw new ArgumentOutOfRangeException("zoomFactor", String.Format("Must not be 0 (epsilon tested with {0})", Tools.Maths.EPSILON_F));
            }
            var sourceHeight = (float)image.Size.Height / zoomFactor;
            var sourceWidth  = (float)image.Size.Width / zoomFactor;
            var startX       = (float)focusX - sourceWidth / 2;
            var startY       = (float)focusY - sourceHeight / 2;

            var sourceRectangle = new CGRect(
                startX,
                startY,
                sourceWidth,
                sourceHeight
                )
                                  .IntersectWith(0, 0, image.Size.Width, image.Size.Height);
            var xClippedRatio = sourceRectangle.Width / sourceWidth;
            var yClippedRatio = sourceRectangle.Height / sourceHeight;

            var destRectangle = new CGRect(CGPoint.Empty, image.Size);

            destRectangle.Width  *= xClippedRatio;
            destRectangle.Height *= yClippedRatio;

            UIImage result;

            using (var sourceImage = Crop(image, sourceRectangle)) {
                UIGraphics.BeginImageContext((CGSize)image.Size);
                var context = UIGraphics.GetCurrentContext();
                context.TranslateCTM(0, image.Size.Height);
                context.ScaleCTM(1f, -1f);
                context.InterpolationQuality = interpolationQuality;
                context.SetAllowsAntialiasing(antiAliasing);
                context.SetFillColor(paddingColor.CGColor);
                context.FillRect(new CGRect(CGPoint.Empty, (CGSize)image.Size));
                context.DrawImage(destRectangle, sourceImage.CGImage);
                result = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
            }

            return(result);
        }
Exemplo n.º 2
0
 public static UIImage FocusZoomAndDispose(
     this UIImage image,
     float zoomFactor,
     int focusX,
     int focusY,
     bool antiAliasing = true,
     CGInterpolationQuality interpolationQuality = CGInterpolationQuality.High,
     UIColor paddingColor = null)
 {
     return(Tools.iOSTool.FocusZoomAndDispose(image, zoomFactor, focusX, focusY, antiAliasing, interpolationQuality, paddingColor));
 }
Exemplo n.º 3
0
 public static UIImage ResizeAndDispose(
     this UIImage image,
     CGSize requestedSize,
     ResizeMethod resizeStrategy     = ResizeMethod.Stretch,
     ResizeAlignment resizeAlignment = ResizeAlignment.CenterCenter,
     bool antiAliasing = true,
     CGInterpolationQuality interpolationQuality = CGInterpolationQuality.High,
     UIColor paddingColor = null)
 {
     return(Tools.iOSTool.ResizeAndDispose(image, requestedSize, resizeStrategy, resizeAlignment, antiAliasing, interpolationQuality, paddingColor));
 }
Exemplo n.º 4
0
 public static UIImage FocusZoomAndDispose(
     UIImage image,
     float zoomFactor,
     int focusX,
     int focusY,
     bool antiAliasing = true,
     CGInterpolationQuality interpolationQuality = CGInterpolationQuality.High,
     UIColor paddingColor = null)
 {
     using (var sourceImage = image) {
         return(FocusZoom(sourceImage, zoomFactor, focusX, focusY));
     }
 }
Exemplo n.º 5
0
        public static ImageInterpolation ToEto(this CGInterpolationQuality value)
        {
            switch (value)
            {
            case CGInterpolationQuality.Default:
                return(ImageInterpolation.Default);

            case CGInterpolationQuality.None:
                return(ImageInterpolation.None);

            case CGInterpolationQuality.Low:
                return(ImageInterpolation.Low);

            case CGInterpolationQuality.Medium:
                return(ImageInterpolation.Medium);

            case CGInterpolationQuality.High:
                return(ImageInterpolation.High);

            default:
                throw new NotSupportedException();
            }
        }
Exemplo n.º 6
0
 extern static void CGContextSetInterpolationQuality(IntPtr context, CGInterpolationQuality quality);
Exemplo n.º 7
0
        public static UIImage Resize(
            UIImage sourceImage,
            CGSize requestedSize,
            ResizeMethod resizeMethod       = ResizeMethod.Stretch,
            ResizeAlignment resizeAlignment = ResizeAlignment.CenterCenter,
            bool antiAliasing = true,
            CGInterpolationQuality interpolationQuality = CGInterpolationQuality.High,
            UIColor paddingColor = null)
        {
            if (paddingColor == null)
            {
                paddingColor = UIColor.Clear;
            }

            var    sourceImageSize = (CGSize)sourceImage.Size;
            var    scaleWidth      = ((float)requestedSize.Width / (float)sourceImageSize.Width);
            var    scaleHeight     = ((float)requestedSize.Height / (float)sourceImageSize.Height);
            CGSize destImageSize;
            CGRect sourceBlitRect      = CGRect.Empty;
            CGRect destBlitRect        = CGRect.Empty;
            bool   alignSourceBlitRect = false;
            bool   alignDestBlitRect   = false;

            switch (resizeMethod)
            {
            case ResizeMethod.AspectFit:
                sourceBlitRect      = new CGRect(CGPoint.Empty, sourceImageSize);
                scaleWidth          = Math.Min(scaleWidth, scaleHeight);
                scaleHeight         = Math.Min(scaleWidth, scaleHeight);
                destBlitRect.Width  = sourceImageSize.Width * scaleWidth;
                destBlitRect.Height = sourceImageSize.Height * scaleHeight;
                destImageSize       = destBlitRect.Size;

                break;

            case ResizeMethod.AspectFitPadded:
                sourceBlitRect      = new CGRect(CGPoint.Empty, sourceImageSize);
                scaleWidth          = Math.Min(scaleWidth, scaleHeight);
                scaleHeight         = Math.Min(scaleWidth, scaleHeight);
                destBlitRect.Width  = sourceImageSize.Width * scaleWidth;
                destBlitRect.Height = sourceImageSize.Height * scaleHeight;
                destImageSize       = requestedSize;
                alignDestBlitRect   = true;
                break;

            case ResizeMethod.AspectFill:
                var sourceAspect = sourceImageSize.Width / sourceImageSize.Height;
                var destAspect   = requestedSize.Width / requestedSize.Height;
                if (destAspect > sourceAspect)
                {
                    sourceBlitRect      = new CGRect(0, 0, sourceImageSize.Width, sourceImageSize.Width / destAspect);
                    alignSourceBlitRect = true;
                }
                else if (destAspect < sourceAspect)
                {
                    sourceBlitRect      = new CGRect(0, 0, sourceImageSize.Height * destAspect, sourceImageSize.Height);
                    alignSourceBlitRect = true;
                }
                else
                {
                    sourceBlitRect = new CGRect(CGPoint.Empty, sourceImageSize);
                }
                destBlitRect  = new CGRect(CGPoint.Empty, requestedSize);
                destImageSize = requestedSize;
                break;

            case ResizeMethod.Stretch:
            default:
                sourceBlitRect      = new CGRect(CGPoint.Empty, sourceImageSize);
                destBlitRect.Width  = sourceImageSize.Width * scaleWidth;
                destBlitRect.Height = sourceImageSize.Height * scaleHeight;
                destImageSize       = requestedSize;
                break;
            }

            if (alignDestBlitRect)
            {
                switch (resizeAlignment)
                {
                case ResizeAlignment.TopLeft:
                    destBlitRect.Offset(0, 0);
                    break;

                case ResizeAlignment.TopCenter:
                    destBlitRect.Offset((destImageSize.Width - destBlitRect.Width) / 2, 0);
                    break;

                case ResizeAlignment.TopRight:
                    destBlitRect.Offset(destImageSize.Width - destBlitRect.Width, 0);
                    break;

                case ResizeAlignment.CenterLeft:
                    destBlitRect.Offset(0, (destImageSize.Height - destBlitRect.Height) / 2);
                    break;

                case ResizeAlignment.CenterCenter:
                    destBlitRect.Offset((destImageSize.Width - destBlitRect.Width) / 2, (destImageSize.Height - destBlitRect.Height) / 2);
                    break;

                case ResizeAlignment.CenterRight:
                    destBlitRect.Offset((destImageSize.Width - destBlitRect.Width), (destImageSize.Height - destBlitRect.Height) / 2);
                    break;

                case ResizeAlignment.BottomLeft:
                    destBlitRect.Offset(0, (destImageSize.Height - destBlitRect.Height));
                    break;

                case ResizeAlignment.BottomCenter:
                    destBlitRect.Offset((destImageSize.Width - destBlitRect.Width) / 2, destImageSize.Height - destBlitRect.Height);
                    break;

                case ResizeAlignment.BottomRight:
                    destBlitRect.Offset(destImageSize.Width - destBlitRect.Width, destImageSize.Height - destBlitRect.Height);
                    break;

                default:
                    break;
                }
            }

            if (alignSourceBlitRect)
            {
                switch (resizeAlignment)
                {
                case ResizeAlignment.TopLeft:
                    sourceBlitRect.Offset(0, 0);
                    break;

                case ResizeAlignment.TopCenter:
                    sourceBlitRect.Offset((sourceImageSize.Width - sourceBlitRect.Width) / 2, 0);
                    break;

                case ResizeAlignment.TopRight:
                    sourceBlitRect.Offset(sourceImageSize.Width - sourceBlitRect.Width, 0);
                    break;

                case ResizeAlignment.CenterLeft:
                    sourceBlitRect.Offset(0, (sourceImageSize.Height - sourceBlitRect.Height) / 2);
                    break;

                case ResizeAlignment.CenterCenter:
                    sourceBlitRect.Offset((sourceImageSize.Width - sourceBlitRect.Width) / 2, (sourceImageSize.Height - sourceBlitRect.Height) / 2);
                    break;

                case ResizeAlignment.CenterRight:
                    sourceBlitRect.Offset((sourceImageSize.Width - sourceBlitRect.Width), (sourceImageSize.Height - sourceBlitRect.Height) / 2);
                    break;

                case ResizeAlignment.BottomLeft:
                    sourceBlitRect.Offset(0, sourceImageSize.Height - sourceBlitRect.Height);
                    break;

                case ResizeAlignment.BottomCenter:
                    sourceBlitRect.Offset((sourceImageSize.Width - sourceBlitRect.Width) / 2, sourceImageSize.Height - sourceBlitRect.Height);
                    break;

                case ResizeAlignment.BottomRight:
                    sourceBlitRect.Offset((sourceImageSize.Width - sourceBlitRect.Width), sourceImageSize.Height - sourceBlitRect.Height);
                    break;

                default:
                    break;
                }
            }

            var imageToUse = alignSourceBlitRect ? Crop(sourceImage, sourceBlitRect) : sourceImage;

            UIGraphics.BeginImageContext(destImageSize);
            var context = UIGraphics.GetCurrentContext();

            //context.TranslateCTM(0, destImageSize.Height);
            //context.ScaleCTM(1f, -1f);
            context.InterpolationQuality = interpolationQuality;
            context.SetAllowsAntialiasing(antiAliasing);
            context.SetFillColor(paddingColor.CGColor);
            context.FillRect(new CGRect(CGPoint.Empty, destImageSize));
            //context.DrawImage(destBlitRect, imageToUse.CGImage);
            imageToUse.Draw(destBlitRect);
            UIImage resizedImage = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            if (imageToUse != sourceImage)
            {
                imageToUse.Dispose();
            }

            return(resizedImage);
        }
Exemplo n.º 8
0
		extern static void CGContextSetInterpolationQuality(IntPtr context, CGInterpolationQuality quality);
Exemplo n.º 9
0
		public static ImageInterpolation ConvertCG (CGInterpolationQuality value)
		{
			switch (value) {
			case CGInterpolationQuality.Default:
				return ImageInterpolation.Default;
			case CGInterpolationQuality.None:
				return ImageInterpolation.None;
			case CGInterpolationQuality.Low:
				return ImageInterpolation.Low;
			case CGInterpolationQuality.Medium:
				return ImageInterpolation.Medium;
			case CGInterpolationQuality.High:
				return ImageInterpolation.High;
			default:
				throw new NotSupportedException();
			}
		}