- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在我的表单应用程序中使用 Xam.Plugin.Media 来拍照。我获取图像流 (GetStream) 并转换为 byte[] 并存储在我的数据库中。然后我将其用作图像源。在 Android 和 UWP 上,它工作正常。在 iOS 上,如果照片是在纵向模式下拍摄的,一旦选择图像,图像将始终旋转 90 度。稍后我会将其上传到服务器,该图像可以在不同的设备上使用。为此,我还尝试了 GetStreamWithImageRotatedForExternalStorage 但在这种情况下,我根本看不到图像。流中有字节(我 DisplayAlert 的长度)但图像不显示。
知道我可能做错了什么吗?
我的代码:-
private async Task TakePicture(WineDetails details)
{
await CrossMedia.Current.Initialize();
if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported)
{
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
AllowCropping = true,
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
SaveToAlbum = false,
RotateImage = true
});
if (file == null)
return;
using (var ms = new MemoryStream())
{
var stream = file.GetStreamWithImageRotatedForExternalStorage();
stream.CopyTo(ms);
details.LabelImage = ms.ToArray();
details.NotifyChange("ImageSource");
}
}
}
图像通过 NotifyChange 在页面中更新,看起来像这样:-ImageSource.FromStream(() => new MemoryStream(this.LabelImage))
这在 Android 和 UWP 上的所有情况下都工作正常,在 iOS 上使用 GetStream 工作(除了图像旋转不正确)但在 iOS 上使用 GetStreamWithImageRotatedForExternalStorage 时不起作用。
还有其他人在使用这个插件吗?知道为什么 GetStream 返回旋转的图像吗?知道为什么 GetStreamWithImageRotatedForExternalStorage 不起作用吗?
谢谢
更新:-
更改了 SaveToAlbum = true,当我打开画廊时,图像旋转了 90 度。有 RotateImage = true 这可能会导致问题吗?我会尝试将其设置为 false。我仍然无法使用 GetStreamWithImageRotatedForExternalStorage 将图像源设置为图像的字节数组。
using (var ms = new MemoryStream())
{
file.GetStreamWithImageRotatedForExternalStorage().CopyTo(ms);
details.LabelImage = ms.ToArray();
}
使用图像的字节数组
return ImageSource.FromStream(() => new MemoryStream(this.LabelImage));
这对我不起作用,GetStream 工作正常。
更新:-
好的,RotateImage = false + GetStreamWithImageRotatedForExternalStorage 允许我显示图像,但它在我的应用程序和图库中仍然错误地旋转。
最佳答案
我正在使用 this插件,它是相似的(如果不是同一件事——我知道 James Montemagno 最近将他的作品与 Xamarin 打包/捆绑在一起)。
如果您查看那里的问题板,您会发现有相当多的人有类似的问题(iOS 上的图像旋转)。几乎每个“解决方案”都提到使用 GetStreamWithImageRotatedForExternalStorage
。
我的问题类似 - 我无法在没有其他(非 iOS 设备)旋转图像的情况下以纵向模式在 iOS 上拍照。我已经尝试了数周来解决这个问题,但对该插件的支持似乎非常有限。
最终我不得不通过一个巨大的解决方法来解决这个问题——使用从FFImageLoading扩展的自定义渲染器。显示我们的图像和MetadataExtractor .然后,我们能够从流中提取 EXIF 数据,并对 FFImageLoding 图像控件应用旋转变换。
旋转信息以一种奇怪的方式存储为字符串。这是我用来提取旋转信息的方法,并将需要旋转的量作为 int 返回。请注意,对我来说,iOS 仍然能够正确显示图像,因此它只返回了 Android 设备的旋转变化。
public static int GetImageRotationCorrection(byte[] image)
{
try
{
var directories = ImageMetadataReader.ReadMetadata(new MemoryStream(image));
if (Device.Android == "Android")
{
foreach (var directory in directories)
{
foreach (var tag in directory.Tags)
{
if (tag.Name == "Orientation")
{
if (tag.Description == "Top, left side(Horizontal / normal)")
return 0;
else if (tag.Description == "Left side, bottom (Rotate 270 CW)")
return 270;
else if (tag.Description == "Right side, top (Rotate 90 CW")
return 90;
}
}
}
}
return 0;
}
catch (Exception ex)
{
return 0;
}
}
请注意,还有一个用于 FFImage 加载的图像的自定义渲染器。
public class RotatedImage : CachedImage
{
public static BindableProperty MyRotationProperty = BindableProperty.Create(nameof(MyRotation), typeof(int), typeof(RotatedImage), 0, propertyChanged: UpdateRotation);
public int MyRotation
{
get { return (int)GetValue(MyRotationProperty); }
set { SetValue(MyRotationProperty, value); }
}
private static void UpdateRotation(BindableObject bindable, object oldRotation, object newRotation)
{
var _oldRotation = (int)oldRotation;
var _newRotation = (int)newRotation;
if (!_oldRotation.Equals(_newRotation))
{
var view = (RotatedImage)bindable;
var transformations = new System.Collections.Generic.List<ITransformation>() {
new RotateTransformation(_newRotation)
};
view.Transformations = transformations;
}
}
}
因此,在我的 XAML 中 - 我声明了一个 RotatedImage 而不是标准图像。使用自定义渲染器,我能够做到这一点并使图像显示旋转正确的量。
image.MyRotation = GetImageRotationCorrection(imageAsBytes)
这是一个完全不必要的解决方法 - 但这些是我为解决此问题所必须付出的努力。
我肯定会继续回答这个问题,社区中可能有人可以帮助我们!
关于ios - Xamarin.Forms Xam.Plugin.Media 在 iOS 上拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51200013/
IO 设备如何知道属于它的内存中的值在memory mapped IO 中发生了变化? ? 例如,假设内存地址 0 专用于保存 VGA 设备的背景颜色。当我们更改 memory[0] 中的值时,VGA
我目前正在开发一个使用Facebook sdk登录(通过FBLoginView)的iOS应用。 一切正常,除了那些拥有较旧版本的facebook的人。 当他们按下“使用Facebook登录”按钮时,他
假设我有: this - is an - example - with some - dashesNSRange将使用`rangeOfString:@“-”拾取“-”的第一个实例,但是如果我只想要最后
Card.io SDK提供以下详细信息: 卡号,有效期,月份,年份,CVV和邮政编码。 如何从此SDK获取国家名称。 - (void)userDidProvideCreditCardInfo:(Car
iOS 应用程序如何从网络服务下载图片并在安装过程中将它们安装到用户的 iOS 设备上?可能吗? 最佳答案 您无法控制应用在用户设备上的安装,因此无法在安装过程中下载其他数据。 只需在安装后首次启动应
我曾经开发过一款企业版 iOS 产品,我们公司曾将其出售给大型企业,供他们的员工使用。 该应用程序通过 AppStore 提供,企业用户获得了公司特定的配置文件(包含应用程序配置文件)以启用他们有权使
我正在尝试将 Card.io SDK 集成到我的 iOS 应用程序中。我想为 CardIO ui 做一个简单的本地化,如更改取消按钮标题或“在此保留信用卡”提示文本。 我在 github 上找到了这个
我正在使用 CardIOView 和 CardIOViewDelegate 类,没有可以设置为 YES 的 BOOL 来扫描 collectCardholderName。我可以看到它在 CardIOP
我有一个集成了通话工具包的 voip 应用程序。每次我从我的 voip 应用程序调用时,都会在 native 电话应用程序中创建一个新的最近通话记录。我在 voip 应用程序中也有自定义联系人(电话应
iOS 应用程序如何知道应用程序打开时屏幕上是否已经有键盘?应用程序运行后,它可以接收键盘显示/隐藏通知。但是,如果应用程序在分屏模式下作为辅助应用程序打开,而主应用程序已经显示键盘,则辅助应用程序不
我在模拟器中收到以下错误: ImageIO: CGImageReadSessionGetCachedImageBlockData *** CGImageReadSessionGetCachedIm
如 Apple 文档所示,可以通过 EAAccessory Framework 与经过认证的配件(由 Apple 认证)进行通信。但是我有点困惑,因为一些帖子告诉我它也可以通过 CoreBluetoo
尽管现在的调试器已经很不错了,但有时找出应用程序中正在发生的事情的最好方法仍然是古老的 NSLog。当您连接到计算机时,这样做很容易; Xcode 会帮助弹出日志查看器面板,然后就可以了。当您不在办公
在我的 iOS 应用程序中,我定义了一些兴趣点。其中一些有一个 Kontakt.io 信标的名称,它绑定(bind)到一个特定的 PoI(我的意思是通常贴在信标标签上的名称)。现在我想在附近发现信标,
我正在为警报提示创建一个 trigger.io 插件。尝试从警报提示返回数据。这是我的代码: // Prompt + (void)show_prompt:(ForgeTask*)task{
您好,我是 Apple iOS 的新手。我阅读并搜索了很多关于推送通知的文章,但我没有发现任何关于 APNS 从 io4 到 ios 6 的新更新的信息。任何人都可以向我提供 APNS 如何在 ios
UITabBar 的高度似乎在 iOS 7 和 8/9/10/11 之间发生了变化。我发布这个问题是为了让其他人轻松找到答案。 那么:在 iPhone 和 iPad 上的 iOS 8/9/10/11
我想我可以针对不同的 iOS 版本使用不同的 Storyboard。 由于 UI 的差异,我将创建下一个 Storyboard: Main_iPhone.storyboard Main_iPad.st
我正在写一些东西,我将使用设备的 iTunes 库中的一部分音轨来覆盖 2 个视频的组合,例如: AVMutableComposition* mixComposition = [[AVMutableC
我创建了一个简单的 iOS 程序,可以顺利编译并在 iPad 模拟器上运行良好。当我告诉 XCode 4 使用我连接的 iPad 设备时,无法编译相同的程序。问题似乎是当我尝试使用附加的 iPad 时
我是一名优秀的程序员,十分优秀!