Xamarin iOS相机和照片 [英] Xamarin iOS camera and photos

查看:207
本文介绍了Xamarin iOS相机和照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS相机拍照,并尝试从图像中提取元数据.这是我的代码:-

I am taking a picture with the iOS camera and trying to extract metadata from the image. This is my code:-

partial void BtnCamera_TouchUpInside(UIButton sender)
        {
            UIImagePickerController imagePicker = new UIImagePickerController();
            imagePicker.PrefersStatusBarHidden();
            imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;

            // handle saving picture and extracting meta-data from picture //
            imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;

            // present //
            PresentViewController(imagePicker, true, () => { });         
        }


protected void Handle_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
        {
            try
            {
                // determine what was selected, video or image
                bool isImage = false;
                switch (e.Info[UIImagePickerController.MediaType].ToString())
                {
                    case "public.image":
                        isImage = true;
                        break;
                }

                // get common info 
                NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceURL")] as NSUrl;
                if (referenceURL != null)
                    Console.WriteLine("Url:" + referenceURL.ToString());

我可以启动相机,拍照,然后单击使用照片" ... referenceURL返回为NULL ...如何获取URL,以便提取GPS的坐标照片和其他这样的属性?

I am able to initiate the camera, take the picture and then however when I click 'use photo'... The referenceURL comes back as NULL... How can I get the url, such that to extract GPS coordinates of the photo and such other attributes ?

推荐答案

您可以从参考URL请求PHAsset,其中将包含一些元数据.您可以请求图像数据以获得更多.

You can request a PHAsset from the reference Url and that will contain some metadata. You can request the image data to obtain more.

注意:如果需要完整的EXIF,则需要检查以确保设备上的图像(可能是基于iCloud的图像),如果需要,请下载它,然后使用ImageIO框架(很多)加载图像数据. SO帖子数量涵盖了这一点.)

Note: If you need full EXIF, you need to check to ensure the image on on the device (could be iCloud-based), download it if needed, and then load the image data with the ImageIO framework (lots of SO postings cover this).

public void ImagePicker_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
    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);
        });
    }

    PHAsset phAsset;
    if (e.ReferenceUrl == null)
    {
        e.OriginalImage?.SaveToPhotosAlbum((image, error) =>
        {
            if (error == null)
            {
                var options = new PHFetchOptions
                {
                    FetchLimit = 1,
                    SortDescriptors = new[] { new NSSortDescriptor("creationDate", true) }
                };
                phAsset = PHAsset.FetchAssets(options).FirstOrDefault() as PHAsset;
                ImageData(phAsset);
            }
        });
    }
    else
    {
        phAsset = PHAsset.FetchAssets(new[] { e.ReferenceUrl }, null).FirstOrDefault() as PHAsset;
        ImageData(phAsset);
    }
}

注意:请确保您已请求运行时照片库授权PHPhotoLibrary.RequestAuthorization),并已在info.plist中设置了Privacy - Photo Library Usage Description字符串,以避免造成严重的隐私崩溃

Note: Make sure you have request runtime photo library authorization PHPhotoLibrary.RequestAuthorization) and have set the Privacy - Photo Library Usage Description string in your info.plist to avoid a nasty privacy crash

这篇关于Xamarin iOS相机和照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆