我正在使用上的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);
});
}发布于 2018-03-13 04:17:29
正如我在您上一篇文章中所说的:Xamarin iOS camera and photos:如果您从相机中捕获照片,事件将不会返回一个ReferenceUrl。但是您可以使用另一个键获取元数据信息:
protected void Handle_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
var metadataInfo = e.Info["UIImagePickerControllerMediaMetadata"]; //or e.MediaMetadata;
}这将包含一些可能对你有帮助的基本信息。但是这个NSDictionary不包含全球定位系统的信息。因为这张图片是自己创建的,所以您应该手动使用CLLocationManager获得全球定位系统:
CLLocationManager manager;
manager = new CLLocationManager();
manager.RequestWhenInUseAuthorization();
manager.LocationsUpdated += (locationSender, e) =>
{
//get current locations
manager.StopUpdatingLocation();
};
manager.StartUpdatingLocation();请注意,如果要部署info.plist添加NSLocationAlwaysUsageDescription,请注意在iOS11上添加iOS10:NSLocationWhenInUseUsageDescription和NSLocationAlwaysAndWhenInUsageDescription中的密钥。
这样,当您从相机中选择照片时,不需要从CGImageSource获取元数据。
https://stackoverflow.com/questions/49244162
复制相似问题