我有一个应用程序,允许用户从资产库中选择多个图像。我遇到的问题是,当我将图像从资产库“复制”到应用程序库中的目录时,没有存储方向元数据。结果是图像有时是颠倒的等等......
下面是来自单元测试的代码。我已将代码简化并移到单独的行中,以便于调试。
这有效,但不复制元数据:
private void getAssetFromURL(ALAsset asset) 
{
    NSError fileError;
    ALAssetRepresentation rep = asset.DefaultRepresentation;
    CGImage cgi = rep.GetImage();
    UIImage img = UIImage.FromImage(cgi);
    NSData jpegData = img.AsJPEG();
    jpegData.Save(pathToSave,true,out fileError);
}
如果我更改代码以包含方向,则会收到对象引用错误。jpegData 为空,当我在调试中查看 UIImage 上的 CGImage 数据时,我注意到它为空。我确实注意到方向确实正确传递给了 UIImage 。
private void getAssetFromURL(ALAsset asset) 
{
    NSError fileError;
    ALAssetRepresentation rep = asset.DefaultRepresentation;
    UIImageOrientation orientation = UIImageOrientation.Up;
    if (rep.Orientation != null && rep.Orientation != ALAssetOrientation.Up)
        orientation = (UIImageOrientation)rep.Orientation;
    CGImage cgi = rep.GetImage();
    UIImage img = UIImage.FromImage(cgi,rep.Scale,orientation);
    NSData jpegData = img.AsJPEG();  //data is null
    jpegData.Save(pathToSave,true,out fileError);  //throws object reference error
}
我究竟做错了什么?有没有一种简单的方法,无需旋转图像即可将方向传递给 JPEG?