简体   繁体   English

Xamarin IOS编码和解码Base64的图像

[英]Xamarin IOS Encoding/Decoding Images from and to Base64

I am working on an application for IOS in Xamarin. 我正在Xamarin中为IOS开发应用程序。 I have a menu in which I request something called "Doublechecks". 我有一个菜单,我在其中请求一个叫做“ Doublechecks”的东西。 These doublechecks have a field with the name "Medication". 这些复选具有名为“ Medication”的字段。 In a previous working copy of the app, I simply used a string to fill this field, but now we had the idea to, instead of filling this medication field with a string, to fill it with an image. 在该应用程序的上一个工作副本中,我只是使用一个字符串来填充该字段,但是现在我们有了一个主意,而不是使用字符串来填充此药物字段,而是使用图像来填充它。 One of the tips I got was to convert a taken or chosen image to base64. 我得到的技巧之一是将拍摄或选择的图像转换为base64。

In the menu where I make a new doublecheck I have a button that pops up an actionsheet, where you can choose weather you want to pick an image from your PhotoLibrary, or take a new picture with your camera. 在我进行新的双重检查的菜单中,我有一个弹出操作表的按钮,您可以在其中选择要从PhotoLibrary中拾取图像的天气,或用相机拍摄新照片。 When you've taken or chosen a picture, I use the following method to encode it to Base64: 当您拍摄或选择图片后,我使用以下方法将其编码为Base64:

var imageToSend = originalImage.AsJPEG (0.23f).GetBase64EncodedString (NSDataBase64EncodingOptions.None);

Where the originalImage is the image I took/chose. 其中originalImage是我拍摄/选择的图像。 Now, when requesting all doublechecks, I use the following to decode it: 现在,当请求所有仔细检查时,我使用以下代码对其进行解码:

byte[] encodedDataAsBytes = System.Convert.FromBase64String (imageToDisplay);
string decoded = System.Text.Encoding.ASCII.GetString (encodedDataAsBytes);
NSData data = NSData.FromString (decoded, NSStringEncoding.ASCIIStringEncoding);
return UIImage.LoadFromData (data);

The method works up until the return. 该方法工作到返回为止。 Somehow, the UIImage is not being constructed, and has its value null, even though the 'data' contains the decoded string. 不知何故,即使“数据”包含已解码的字符串,也不会构造UIImage,并且它的值为null。

I have searched several threads and fora, but without much avail. 我已经搜索了多个线程和论坛,但没有多大用处。 Any help would be much appreciated. 任何帮助将非常感激。

Why are you converting the base64 decoded data into a string ? 为什么将base64 解码的数据转换为string

Since you already have the byte array you should be able to simply do: 由于您已经拥有字节数组,因此您应该可以简单地执行以下操作:

byte[] encodedDataAsBytes = System.Convert.FromBase64String (imageToDisplay);
NSData data = NSData.FromArray (encodedDataAsBytes);
return UIImage.LoadFromData (data);

NSData method not always a good solution. NSData方法并不总是一个好的解决方案。 You can also try following 您也可以尝试关注

var rawData = pixelMap.ToByteArray();

        using (var provider = new CGDataProvider(rawData, 0, rawData.Length))
        {
            using (var colorSpace = CGColorSpace.CreateDeviceRGB())
            {
                var cgImage = new CGImage(
                    pixelMap.Width,
                    pixelMap.Height,
                    pixelMap.BitsPerComponent,
                    pixelMap.BytesPerPixel * ByteToBit,
                    pixelMap.BytesPerPixel * pixelMap.Width,
                    colorSpace,
                    CGBitmapFlags.ByteOrderDefault,
                    provider,
                    null,
                    true,
                    CGColorRenderingIntent.Default
                );

                return ImageSource.FromStream(() => UIImage.FromImage(cgImage).AsPNG().AsStream());
            }
        }

Check out my library and samples for more info about both Android and iOS bitmap from/to 2d pixel map. 查看我的资料库和示例,以获取有关从/到2d像素图的Android和iOS位图的更多信息。 https://github.com/enginkirmaci/Imaging-Library https://github.com/enginkirmaci/Imaging-Library

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM