首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS CGImageFromUrl返回null

iOS CGImageFromUrl返回null
EN

Stack Overflow用户
提问于 2018-03-12 20:47:53
回答 1查看 261关注 0票数 1

我正在使用上的Xamarin iOS从iphone相机拍摄的照片中提取元数据。

代码语言:javascript
复制
 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的方法。

代码语言:javascript
复制
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);

                    });
                }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-13 04:17:29

正如我在您上一篇文章中所说的:Xamarin iOS camera and photos:如果您从相机中捕获照片,事件将不会返回一个ReferenceUrl。但是您可以使用另一个键获取元数据信息:

代码语言:javascript
复制
protected void Handle_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
    var metadataInfo = e.Info["UIImagePickerControllerMediaMetadata"]; //or e.MediaMetadata;
}

这将包含一些可能对你有帮助的基本信息。但是这个NSDictionary不包含全球定位系统的信息。因为这张图片是自己创建的,所以您应该手动使用CLLocationManager获得全球定位系统:

代码语言:javascript
复制
CLLocationManager manager;
manager = new CLLocationManager();
manager.RequestWhenInUseAuthorization();

manager.LocationsUpdated += (locationSender, e) =>
{

    //get current locations
    manager.StopUpdatingLocation();
};
manager.StartUpdatingLocation();

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49244162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档