我试图从手机上传图片到我的web服务和我注意到,当我上传图像的图像定位丢失。在上传之前是否需要执行某些操作以确保图像以正确的方向上传?旋转的UIImage的MonoTouch
我还研究了其他地方,发现目标-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;
}
将方向“丢失”通过上传事实似乎表明,无论是EXIF方向标签不再附加到图像上,或者接收软件不遵守它。你有没有探索过这种可能性? –
我会先探讨Jacob Foshee关于EXIF方向标签的建议。看到http://stackoverflow.com/questions/9766394/get-exif-data-from-uiimage-uiimagepickercontroller – Diego
你救了我的一天。谢谢 –