我試圖從手機上傳圖片到我的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
你救了我的一天。謝謝 –