我正在使用XMIrin ios的GMImagePickerController来允许用户从他们的图库中选择多个图像。我在回调函数中处理这些图像时遇到问题。尝试将UIImages转换为字节数组时,我得到一个空引用异常。
var picker = new GMImagePickerController
{
ColsInPortrait = 3,
ColsInLandscape = 5,
ModalPresentationStyle = UIModalPresentationStyle.Popover,
AllowsMultipleSelection = true,
};
PHImageManager imageManager = new PHImageManager();
picker.FinishedPickingAssets += (sender, ev) =>
{
List<Byte[]> images = new List<Byte[]>();
foreach (var asset in picker.SelectedAssets)
{
imageManager.RequestImageForAsset(asset,
new CGSize(asset.PixelWidth, asset.PixelHeight),
PHImageContentMode.Default,
null,
(UIImage image, NSDictionary info) =>
{
uiImages.Add(image);
});
}
picker.DismissModalViewController(true);
foreach(var i in uiImages)
{
Byte[] imgArray;
using (NSData imgData = i.AsJPEG())
{
imgArray = new Byte[imgData.Length];
System.Runtime.InteropServices.Marshal.Copy(imgData.Bytes, imgArray, 0, Convert.ToInt32(imgData.Length));
images.Add(imgArray);
}
}
callback(images);
};
我在上一个foreach循环中遇到异常当我尝试NSData imgData = i.AsJPEG()时,imgData保持为null。但是我已经检查过了,此时我不是空的。谁能解释为什么i.ASJPEG()会返回null?
关于如何将我选择的资产转换为字节数组以传递给我的回调函数的任何建议都将不胜感激。
谢谢!