【发布时间】:2018-08-21 00:00:34
【问题描述】:
我在 Windows Visual Studio 上使用 Xamarin iOS 从 iphone 相机拍摄的照片中提取元数据。
private void GetMetaData(NSUrl url)
{
CGImageSource myImageSource;
myImageSource = CGImageSource.FromUrl(url, null);
var ns = new NSDictionary();
var imageProperties = myImageSource.CopyProperties(ns, 0);
var gps = imageProperties.ObjectForKey(CGImageProperties.GPSDictionary) as NSDictionary;
var lat = gps[CGImageProperties.GPSLatitude];
var latref = gps[CGImageProperties.GPSLatitudeRef];
var lon = gps[CGImageProperties.GPSLongitude];
var lonref = gps[CGImageProperties.GPSLongitudeRef];
var loc = String.Format("GPS: {0} {1}, {2} {3}", lat, latref, lon, lonref);
Console.WriteLine(loc);
}
传递给方法的 url 如下:- {file:///var/mobile/Media/DCIM/100APPLE/IMG_0006.JPG}
CGImageSource.FromUrl(url, null) 返回 null 并且我的应用程序崩溃...谁能向我解释我需要如何解决这个问题?
编辑这就是我获取图像 URL 的方式。
protected void Handle_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
NSUrl url = null;
try
{
void ImageData(PHAsset asset)
{
if (asset == null) throw new Exception("PHAsset is null");
PHImageManager.DefaultManager.RequestImageData(asset, null, (data, dataUti, orientation, info) =>
{
//Console.WriteLine(data);
Console.WriteLine(info);
url = info.ValueForKey(new NSString("PHImageFileURLKey")) as NSUrl;
// Call method to get MetaData from Image Url //
GetMetaData(url);
});
}
【问题讨论】:
-
在可疑行周围放置一个 try/catch 以获取异常。很可能它是一个无效的 url - 你是从哪里想出这条路径的?如果您想从照片库中的图像中读取数据,那不是这样做的。
-
@Jason 和我使用 PHAsset 提取了路径
-
另外,就像我说的那样,这是一个空异常..未设置对象引用..
-
我猜 NSUrl 在给定无效参数时返回 null - 您需要进行一些调试以追踪 null ref 的来源
标签: ios visual-studio xamarin xamarin.ios photo