Xamarin iOS CGImageFromUrl returns 空
Xamarin iOS CGImageFromUrl returns null
我在 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) returns 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);
});
}
正如我在上次post中所说::如果您使用相机拍摄照片,事件将不会return ReferenceUrl
。但是您可以使用另一个键获取元数据信息:
protected void Handle_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
var metadataInfo = e.Info["UIImagePickerControllerMediaMetadata"]; //or e.MediaMetadata;
}
这将包含一些可能对您有所帮助的基本信息。但是这个 NSDictionary
不包含 GPS 信息。因为这张图是自己做的,所以需要手动使用CLLocationManager
获取GPS:
CLLocationManager manager;
manager = new CLLocationManager();
manager.RequestWhenInUseAuthorization();
manager.LocationsUpdated += (locationSender, e) =>
{
//get current locations
manager.StopUpdatingLocation();
};
manager.StartUpdatingLocation();
请注意在 info.plist
中添加密钥:NSLocationWhenInUseUsageDescription
和 NSLocationAlwaysAndWhenInUsageDescription
on iOS11,如果要部署 iOS10 添加 NSLocationAlwaysUsageDescription
.
这样,当您从相机中选取照片时,就不需要从 CGImageSource
获取元数据了。
我在 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) returns 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);
});
}
正如我在上次post中所说:ReferenceUrl
。但是您可以使用另一个键获取元数据信息:
protected void Handle_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
var metadataInfo = e.Info["UIImagePickerControllerMediaMetadata"]; //or e.MediaMetadata;
}
这将包含一些可能对您有所帮助的基本信息。但是这个 NSDictionary
不包含 GPS 信息。因为这张图是自己做的,所以需要手动使用CLLocationManager
获取GPS:
CLLocationManager manager;
manager = new CLLocationManager();
manager.RequestWhenInUseAuthorization();
manager.LocationsUpdated += (locationSender, e) =>
{
//get current locations
manager.StopUpdatingLocation();
};
manager.StartUpdatingLocation();
请注意在 info.plist
中添加密钥:NSLocationWhenInUseUsageDescription
和 NSLocationAlwaysAndWhenInUsageDescription
on iOS11,如果要部署 iOS10 添加 NSLocationAlwaysUsageDescription
.
这样,当您从相机中选取照片时,就不需要从 CGImageSource
获取元数据了。