在 Xamarin iOS 中将 HEIC 图像转换为 Jpg 并上传到服务器 [英] Converting an HEIC image to Jpg and upload to the Server in Xamarin iOS

查看:12
本文介绍了在 Xamarin iOS 中将 HEIC 图像转换为 Jpg 并上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用依赖服务将 HEIC 图像从下面的代码转换为 Jpg,并尝试使用图像显示并上传到 Web API ..但是两者都不起作用,这是将 HEIC 转换为 Jpg 的正确方法吗?如果没有,请建议我如何实现这一目标.

I am trying to convert the HEIC image to Jpg from below code in using Dependency Service and trying to display with Image and uploading to Web API.. But both are not working, Is it a correct way of doing HEIC conversion to Jpg? if not please suggest me how to achieve this.

依赖服务方法:

public byte[] GetJpgFromHEIC(string path)
{
        byte[] imageAsBytes = File.ReadAllBytes(path);
        UIImage images = new UIImage(NSData.FromArray(imageAsBytes));
        byte[] bytes = images.AsJPEG().ToArray();
        // Stream imgStream = new MemoryStream(bytes);

        return bytes;
}

在显示图像背后的 Xaml 代码中:

In Xaml Code Behind Displaying Image:

Image image = new Image(){};
byte[] bytes = DependencyService.Get<ICommonHelper>
                      ().GetJpgFromHEIC(fileData.FileName);
image.Source = ImageSource.FromStream(() => new MemoryStream(bytes));

将代码上传到 Web API:在 StreamContent 和 ByteArrayContent 作为 HttpContent 尝试如下

Uploading code to web API: Tried in both StreamContent and ByteArrayContent as HttpContent like below

   HttpResponseMessage response = null;

    string filename = data.FileName; // here data object is a FileData, picked from using FilePicker.

    byte[] fileBytes = DependencyService.Get<ICommonHelper>().GetJpgFromHEIC(data.FilePath);

            HttpContent fileContent = new ByteArrayContent(fileBytes);

    // Stream fileStream = new MemoryStream(fileBytes);
    // HttpContent fileContent = new StreamContent(fileStream);

    fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name =         
             "file", FileName = data.FileName };
    fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
            using (var formData = new MultipartFormDataContent())
            {
                formData.Add(fileContent);
                response = await client.PostAsync(uri, formData);
                if (response.IsSuccessStatusCode)
                {
                    Debug.WriteLine(@" Upload CommentsAttachments SUCCESS>> " + response.Content.ToString());
                }
            }

请告诉我我做错了什么以及如何实现这种转换和上传的可能方法.

Please suggest me What I am doing wrong and how to achieve and possible ways for this conversion and upload.

谢谢,

推荐答案

类似的东西适用于 HEIC 路径/文件(或任何有效的 iOS 支持的图像格式).我正在使用示例 autumn_1440x960.heic.

Something like this works for a HEIC path/file (or any valid iOS supported image format). I'm using the sample autumn_1440x960.heic.

using (var image = UIImage.FromFile(path))
using (var jpg = image.AsJPEG(0.5f))
using (var stream = jpg.AsStream())
{
    // do something with your stream, just saving it to the cache directory as an example...

    using (var cache = NSFileManager.DefaultManager.GetUrl(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomain.All, null, true, out var nsError))
    using (var fileStream = new FileStream(Path.Combine(cache.Path, "cache.jpg"), FileMode.Create, FileAccess.Write))
    {
        stream.CopyTo(fileStream);
        imageView1.Image = UIImage.FromFile(Path.Combine(cache.Path, "cache.jpg"));
    }
}

仅供参考:复制 byte[] 的效率非常低,您可能只想将原始流传回并使用它来填充要发布的表单内容,只需让您处理它,否则您将泄漏本机分配...

FYI: Copying byte[] around is very inefficient, you might just want to pass the original stream back and use that to populate your form content to post, just make you Dispose it otherwise you will leak native allocations...

这篇关于在 Xamarin iOS 中将 HEIC 图像转换为 Jpg 并上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆