Converting an HEIC image to Jpg and upload to the Server in Xamarin iOS

て烟熏妆下的殇ゞ 提交于 2019-12-08 09:32:58

问题


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.

Dependency Service method:

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;
}

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));

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.

Thank you,


回答1:


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"));
    }
}

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...



来源:https://stackoverflow.com/questions/54103811/converting-an-heic-image-to-jpg-and-upload-to-the-server-in-xamarin-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!