似乎当我用相机在纵向模式下拍照时,UIImage有正确的尺寸/高宽比(1536x2048 / 3:4)和方向(右),导出到一个文件(用UIImage.AsPNG().Save()),它总是以景观模式出现(2048x1536,4:3)。
这是真的,还是我做错了什么?有什么解决办法吗,例如ALAssetsLibrary.WriteImageToSavedPhotosAlbum?
Update:最初我以为UIImage.SaveToPhotosAlbum()也会发生这种情况,但是仔细检查后我发现,在这种情况下,UIImage不是来自摄像机的原始数据,而是从早期AsPNG()保存的数据中重构出来的。
再次更新:看起来这是iPhone相机的一个一般的“特性”,唯一的修复方法就是手动旋转图像。我尝试将这段代码移植到MT,如下所示,但我似乎已经发明了一种新的方法来创建合适大小和纵横比的空白图像。有人能发现窃听器吗?
public static class ImageUtils
{
static float DefaultMaxSize = 960;
public static UIImage ScaleAndRotateImage (UIImage image) {
return ScaleAndRotateImage(image, DefaultMaxSize);
}
public static UIImage ScaleAndRotateImage (UIImage image, float maxSize)
{
CGImage imgRef = image.CGImage;
float width = imgRef.Width;
float height = imgRef.Height;
CGAffineTransform transform = CGAffineTransform.MakeIdentity();
RectangleF bounds = new RectangleF (0, 0, width, height);
if (width > maxSize || height > maxSize) {
float ratio = width / height;
if (ratio > 1) {
bounds.Width = maxSize;
bounds.Height = bounds.Width / ratio;
} else {
bounds.Height = maxSize;
bounds.Width = bounds.Height * ratio;
}
}
float scaleRatio = bounds.Width / width;
SizeF imageSize = new SizeF (imgRef.Width, imgRef.Height);
float boundHeight;
UIImageOrientation orient = image.Orientation;
if (orient == UIImageOrientation.Up) {
//EXIF = 1
transform = CGAffineTransform.MakeIdentity();
} else if (orient == UIImageOrientation.UpMirrored) {
//EXIF = 2
transform = CGAffineTransform.MakeTranslation (imageSize.Width, 0);
transform.Scale (-1.0f, 1.0f);
} else if (orient == UIImageOrientation.Down) {
//EXIF = 3
transform = CGAffineTransform.MakeTranslation (imageSize.Width, imageSize.Height);
transform.Rotate ((float) Math.PI);
} else if (orient == UIImageOrientation.DownMirrored) {
//EXIF = 4
transform = CGAffineTransform.MakeTranslation (0, imageSize.Height);
transform.Scale (1.0f, -1.0f);
} else if (orient == UIImageOrientation.LeftMirrored) {
//EXIF = 5
boundHeight = bounds.Height;
bounds.Height = bounds.Width;
bounds.Width = boundHeight;
transform = CGAffineTransform.MakeTranslation (imageSize.Height, imageSize.Width);
transform.Scale (-1.0f, 1.0f);
transform.Rotate ((float)(3.0f * Math.PI / 2.0));
} else if (orient == UIImageOrientation.Left) {
//EXIF = 6
boundHeight = bounds.Height;
bounds.Height = bounds.Width;
bounds.Width = boundHeight;
transform = CGAffineTransform.MakeTranslation (0, imageSize.Width);
transform.Rotate ((float)(3.0f * Math.PI / 2.0));
} else if (orient == UIImageOrientation.RightMirrored) {
//EXIF = 7
boundHeight = bounds.Height;
bounds.Height = bounds.Width;
bounds.Width = boundHeight;
transform = CGAffineTransform.MakeScale (-1.0f, 1.0f);
transform.Rotate ((float)(Math.PI / 2.0));
} else if (orient == UIImageOrientation.Right) {
//EXIF = 8
boundHeight = bounds.Height;
bounds.Height = bounds.Width;
bounds.Width = boundHeight;
transform = CGAffineTransform.MakeTranslation (imageSize.Height, 0);
transform.Rotate ((float)(Math.PI / 2.0));
} else {
throw new InvalidOperationException ("Invalid image orientation");
}
UIGraphics.BeginImageContext(bounds.Size);
CGContext context = UIGraphics.GetCurrentContext ();
if (orient == UIImageOrientation.Right || orient == UIImageOrientation.Left) {
context.ScaleCTM (-scaleRatio, scaleRatio);
context.TranslateCTM (-height, 0f);
} else {
context.ScaleCTM (scaleRatio, -scaleRatio);
context.TranslateCTM (0f, -height);
}
context.ConcatCTM(transform);
context.DrawImage (new RectangleF(0, 0, width, height), imgRef);
UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
return imageCopy;
}
}发布于 2011-03-28 13:41:54
我终于把这个搞定了。下面是一个包含代码的要点:
https://gist.github.com/890460
我的博客也是这样写的:
http://www.fastchicken.co.nz/2011/03/28/scaling-and-rotating-an-image-in-monotouch/
发布于 2011-02-18 00:00:16
在这里找到了一个答案:CGContextDrawImage在传递UIImage.CGImage时将图像颠倒绘制:使用UIImage.Draw()而不是CGContext.DrawImage()。由此产生的代码:
public static UIImage Rotate(UIImage orig) {
float w0 = orig.Size.Width;
float h0 = orig.Size.Height;
UIGraphics.BeginImageContext(new SizeF(w0, h0));
orig.Draw(new RectangleF(0, 0, w0, h0));
UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
return imageCopy;
}这产生了一个正确的大小,纵横比和方向的图像,但与方向总是“向上”。
发布于 2011-01-19 18:57:28
尝试通过UIImage将NSData转换为UIImageJPEGRepresentation,因为PNG映像不保存图像方向值。
https://stackoverflow.com/questions/4739231
复制相似问题