【问题标题】:Xamarin iOS CGImageFromUrl returns nullXamarin iOS CGImageFromUrl 返回 null
【发布时间】: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


【解决方案1】:

正如我在您上一篇文章中所说:Xamarin iOS camera and photos:如果您从相机拍摄照片,该事件将不会返回 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();

请注意在iOS11上添加info.plist:NSLocationWhenInUseUsageDescriptionNSLocationAlwaysAndWhenInUsageDescription中的键,如果要部署iOS10添加NSLocationAlwaysUsageDescription

这样,当您从相机中挑选照片时,无需从CGImageSource 获取元数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 2020-07-15
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多