2
我有一個應用程序,允許用戶從資產庫中選擇多個圖像。我遇到的問題是,當我將資產庫中的圖像「複製」到我的應用程序庫的目錄中時,未存儲方向元數據。結果是圖像有時會顛倒等等。monotouch資產庫方向
以下是單元測試的代碼。我簡化了代碼,並將代碼移到單獨的行中以便於調試。
這工作,但不復制元數據:如果我更改代碼,包括定向
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?