gpt4 book ai didi

iphone - UIImage 数据始终处于横向模式

转载 作者:行者123 更新时间:2023-12-03 20:28:58 33 4
gpt4 key购买 nike

似乎当我在纵向模式下使用相机拍照时,UIImage 具有正确的尺寸/长宽比(1536x2048/3:4)和方向(右),导出到一个文件(使用 UIImage.AsPNG().Save()),它始终以横向模式显示(2048x1536,4:3)。

这是真的吗,还是我做错了什么?有没有解决方法,例如与ALAssetsLibrary.WriteImageToSavedPhotosAlbum

更新: 最初我认为 UIImage.SaveToPhotosAlbum() 也会发生这种情况,但仔细检查后,我意识到 UIImage 中的案例不是来自相机的原始案例,而是根据早期 AsPNG() 保存的数据重建的案例。

再次更新:看起来这是 iPhone 相机的一般“功能”,修复它的唯一方法是手动旋转图像。我尝试移植this code到 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;
}
}

最佳答案

我终于让它发挥作用了。这是包含代码的要点:

https://gist.github.com/890460

以及我关于它的博客文章:

http://www.fastchicken.co.nz/2011/03/28/scaling-and-rotating-an-image-in-monotouch/

关于iphone - UIImage 数据始终处于横向模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4739231/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com