旋转UIImage Monotouch

时间:2012-09-17 02:04:21

标签: ios xamarin.ios

我尝试将图片从手机上传到我的网络服务,并注意到上传图片时图片方向已丢失。在上传之前我是否需要做些什么才能确保以正确的方向上传图片?

我也在其他地方看了一下,发现了Objective-C代码来旋转我转换成C#的图像,但是每次使用旋转方法时,图像都会变黑,即我猜不出任何东西。

我附上我的代码供您参考,如果有人能告诉我我做错了什么,我真的很感激。谢谢!

    public static UIImage RotateImage(this UIImage image)
    {
        UIImage imageToReturn = null;
        if(image.Orientation == UIImageOrientation.Up)
        {
            imageToReturn = image;
        }
        else 
        {
            CGAffineTransform transform = CGAffineTransform.MakeIdentity();

            switch (image.Orientation) {
                case UIImageOrientation.Down:
                case UIImageOrientation.DownMirrored:
                    transform.Translate(image.Size.Width, image.Size.Height);
                    transform.Rotate((float)Math.PI);
                    break;

                case UIImageOrientation.Left:
                case UIImageOrientation.LeftMirrored:
                    transform.Translate(image.Size.Width, 0);
                    transform.Rotate((float)Math.PI/2);
                    break;

                case UIImageOrientation.Right:
                case UIImageOrientation.RightMirrored:
                    transform.Translate(0, image.Size.Height);
                    transform.Rotate((float)-Math.PI/2);
                    break;
                case UIImageOrientation.Up:
                case UIImageOrientation.UpMirrored:
                    break;
            }

            switch (image.Orientation) {
                case UIImageOrientation.UpMirrored:
                case UIImageOrientation.DownMirrored:
                    transform.Translate(image.Size.Width, 0);
                    transform.Scale(-1, 1);
                    break;

                case UIImageOrientation.LeftMirrored:
                case UIImageOrientation.RightMirrored:
                    transform.Translate(image.Size.Height, 0);
                    transform.Scale(-1, 1);
                    break;
                case UIImageOrientation.Up:
                case UIImageOrientation.Down:
                case UIImageOrientation.Left:
                case UIImageOrientation.Right:
                    break;
            }

            //now draw image
            using(var context = new CGBitmapContext(IntPtr.Zero,
                                                    (int)image.Size.Width, 
                                                    (int)image.Size.Height, 
                                                    image.CGImage.BitsPerComponent,
                                                    image.CGImage.BytesPerRow,
                                                    image.CGImage.ColorSpace,
                                                    image.CGImage.BitmapInfo)){
                context.ConcatCTM(transform);
                switch (image.Orientation) 
                {
                    case UIImageOrientation.Left:
                    case UIImageOrientation.LeftMirrored:
                    case UIImageOrientation.Right:
                    case UIImageOrientation.RightMirrored:
                        // Grr...
                        context.DrawImage(new RectangleF(PointF.Empty,new SizeF(image.Size.Height, image.Size.Width)), image.CGImage);
                        break;
                    default:
                        context.DrawImage(new RectangleF(PointF.Empty, new SizeF(image.Size.Width, image.Size.Height)), image.CGImage);
                        break;
                }

                using(var imageRef = context.ToImage())
                {
                    imageToReturn = new UIImage(imageRef);
                }
            }
        }

        return imageToReturn;
    }

3 个答案:

答案 0 :(得分:3)

你获得黑色图像的原因是翻译和旋转调用是向后的。 iPod / iphone上的普通人像照片处于“正确”方向。转换它们的正确代码使用:

        case UIImageOrientation.Right:
        case UIImageOrientation.RightMirrored:
            transform.Rotate (-(float)Math.PI / 2);
            transform.Translate (0, input.Size.Height);
            break;

转换函数依赖于顺序。此代码将其旋转到正确的方向,但由于旋转大约是左下方的0,0坐标,因此图像现在刚好在框架的底部。翻译然后将它推到它所属的位置。

原始代码会将侧面图像从框架顶部推开,然后旋转会使图像向右转,但是会偏离框架的右侧。

使用较小的高度值,如100或200和pi / 4,可以轻松显示更改这些功能的顺序时所获得的差异,因为某些原始图像将始终可见。

答案 1 :(得分:1)

使用@Random提供的信息,我更正了原始代码:

    private byte[] RotateImage(UIImage image)
    {
        UIImage imageToReturn = null;
        if (image.Orientation == UIImageOrientation.Up)
        {
            imageToReturn = image;
        }
        else
        {
            CGAffineTransform transform = CGAffineTransform.MakeIdentity();

            switch (image.Orientation)
            {
                case UIImageOrientation.Down:
                case UIImageOrientation.DownMirrored:
                    transform.Rotate((float)Math.PI);
                    transform.Translate(image.Size.Width, image.Size.Height);
                    break;

                case UIImageOrientation.Left:
                case UIImageOrientation.LeftMirrored:
                    transform.Rotate((float)Math.PI / 2);
                    transform.Translate(image.Size.Width, 0);
                    break;

                case UIImageOrientation.Right:
                case UIImageOrientation.RightMirrored:
                    transform.Rotate(-(float)Math.PI / 2);
                    transform.Translate(0, image.Size.Height);
                    break;
                case UIImageOrientation.Up:
                case UIImageOrientation.UpMirrored:
                    break;
            }

            switch (image.Orientation)
            {
                case UIImageOrientation.UpMirrored:
                case UIImageOrientation.DownMirrored:
                    transform.Translate(image.Size.Width, 0);
                    transform.Scale(-1, 1);
                    break;

                case UIImageOrientation.LeftMirrored:
                case UIImageOrientation.RightMirrored:
                    transform.Translate(image.Size.Height, 0);
                    transform.Scale(-1, 1);
                    break;
                case UIImageOrientation.Up:
                case UIImageOrientation.Down:
                case UIImageOrientation.Left:
                case UIImageOrientation.Right:
                    break;
            }

            //now draw image
            using (var context = new CGBitmapContext(IntPtr.Zero,
                                                    (int)image.Size.Width,
                                                    (int)image.Size.Height,
                                                    image.CGImage.BitsPerComponent,
                                                    image.CGImage.BytesPerRow,
                                                    image.CGImage.ColorSpace,
                                                    image.CGImage.BitmapInfo))
            {
                context.ConcatCTM(transform);
                switch (image.Orientation)
                {
                    case UIImageOrientation.Left:
                    case UIImageOrientation.LeftMirrored:
                    case UIImageOrientation.Right:
                    case UIImageOrientation.RightMirrored:
                        // Grr...
                        context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)image.Size.Height, (float)image.Size.Width)), image.CGImage);
                        break;
                    default:
                        context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)image.Size.Width, (float)image.Size.Height)), image.CGImage);
                        break;
                }

                using (var imageRef = context.ToImage())
                {
                    imageToReturn = new UIImage(imageRef);
                }
            }
        }

        using (NSData imageData = imageToReturn.AsJPEG())
        {
            Byte[] byteArray = new Byte[imageData.Length];
            System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, byteArray, 0, Convert.ToInt32(imageData.Length));
            return byteArray;
        }
    }

非常有用的代码

答案 2 :(得分:0)

我通过this gist找到了this question。希望这段代码适合你!