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

问题描述

我正在尝试使用依赖服务从下面的代码将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...

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

09-19 00:13