Xamarin iOS camera and photos

二次信任 提交于 2019-12-08 09:03:32

问题


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());

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 ?


回答1:


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

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);
    }
}

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



来源:https://stackoverflow.com/questions/49203247/xamarin-ios-camera-and-photos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!