Ios 旋转图像单触
我正试图从手机上传一幅图像到我的网络服务,我注意到上传图像时图像方向丢失了。在上传之前,我需要做些什么来确保上传的图像方向正确吗 我还查看了其他地方,发现objective-C代码用于旋转图像,我将其转换为C#,但每次使用旋转方法时,图像都会变为黑色,也就是说,我猜不会显示任何内容 我附上我的代码供您参考,如果有人能告诉我我做错了什么,我将非常感谢。谢谢大家!Ios 旋转图像单触,ios,xamarin.ios,Ios,Xamarin.ios,我正试图从手机上传一幅图像到我的网络服务,我注意到上传图像时图像方向丢失了。在上传之前,我需要做些什么来确保上传的图像方向正确吗 我还查看了其他地方,发现objective-C代码用于旋转图像,我将其转换为C#,但每次使用旋转方法时,图像都会变为黑色,也就是说,我猜不会显示任何内容 我附上我的代码供您参考,如果有人能告诉我我做错了什么,我将非常感谢。谢谢大家! public static UIImage RotateImage(this UIImage image) {
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;
}
我找到了维娅。希望这段代码适合你 出现黑色图像的原因是平移和旋转调用是向后的。iPod/iphone上的普通肖像图片是“右”方向的。转换它们的正确代码使用:
case UIImageOrientation.Right:
case UIImageOrientation.RightMirrored:
transform.Rotate (-(float)Math.PI / 2);
transform.Translate (0, input.Size.Height);
break;
变换函数是顺序相关的。此代码将其旋转到正确的方向,但由于旋转大约在左下角的0,0坐标处,因此图像现在刚好离开帧的底部。然后,translate将其向上推到它所属的位置
原始代码会将侧面图像推离帧的顶部,然后旋转会使图像向右旋转,但远离帧的右侧
使用较小的高度值,如100或200和pi/4,可以很容易地显示更改这些函数顺序时得到的差异,因为某些原始图像始终可见。使用@Random I提供的信息更正原始代码以使其正常工作:
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;
}
}
非常有用的一段代码上传时方向“丢失”这一事实似乎表明EXIF方向标签不再附加到图像上,或者接收软件不支持它。你探索过这种可能性吗?我会首先探索Jacob Foshee关于EXIF方向标签的建议。看你救了我一天。谢谢