我正在尝试从手机上传图像到我的web服务,但我注意到当我上传图像时图像方向丢失。上传之前是否需要做些什么来确保图片的上传方向正确?
我还查看了其他地方,发现objective-C代码可以旋转我转换为C#的图像,但每次使用Rotate方法时,图像都会变黑,即我猜没有显示任何内容。
我附上我的代码供你参考,如果有人能告诉我我做错了什么,我会非常,非常感谢。谢谢!
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;
}发布于 2013-03-08 03:41:39
你得到黑色图像的原因是平移和旋转调用是向后的。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,可以很容易地显示出更改这些函数的顺序时得到的差异,因为一些原始图像将始终可见。
发布于 2017-07-24 19:57:56
使用@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;
}
}一段非常有用的代码
发布于 2013-02-19 17:34:55
我通过this question找到了this gist。希望这段代码能为你工作!
https://stackoverflow.com/questions/12452358
复制相似问题