Xamarin iOS相机和照片



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

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

我可以启动相机,拍照,然后当我单击"使用照片"时......引用网址返回为空...如何获取网址,以便提取照片的GPS坐标和此类其他属性?

我在URL上遇到了很大的麻烦。它可以是一个文件,它可以是一个 Web URL,它在每个设备上的行为都不同。我的应用程序崩溃并与我的测试组一起烧毁了很多次。我终于找到了一种从数据中获取元数据的方法。有多种方法可以获取拍摄日期,宽度和高度以及GPS坐标。此外,我还需要相机MFG和模型。

string dateTaken = string.Empty;
string lat = string.Empty;
string lon = string.Empty;
string width = string.Empty;
string height = string.Empty;
string mfg = string.Empty;
string model = string.Empty;
PHImageManager.DefaultManager.RequestImageData(asset, options, (data, dataUti, orientation, info) => {
    dateTaken = asset.CreationDate.ToString();
    // GPS Coordinates
    var coord = asset.Location?.Coordinate;
    if (coord != null)
    {
        lat = asset.Location?.Coordinate.Latitude.ToString();
        lon = asset.Location?.Coordinate.Longitude.ToString();
    }
    UIImage img = UIImage.LoadFromData(data);
    if (img.CGImage != null)
    {
        width = img.CGImage?.Width.ToString();
        height = img.CGImage?.Height.ToString();
    }
    using (CGImageSource imageSource = CGImageSource.FromData(data, null))
    {
        if (imageSource != null)
        {
            var ns = new NSDictionary();
            var imageProperties = imageSource.CopyProperties(ns, 0);
            if (imageProperties != null)
            {
                width = ReturnStringIfNull(imageProperties[CGImageProperties.PixelWidth]);
                height = ReturnStringIfNull(imageProperties[CGImageProperties.PixelHeight]);
                var tiff = imageProperties.ObjectForKey(CGImageProperties.TIFFDictionary) as NSDictionary;
                if (tiff != null)
                {
                    mfg = ReturnStringIfNull(tiff[CGImageProperties.TIFFMake]);
                    model = ReturnStringIfNull(tiff[CGImageProperties.TIFFModel]);
                    //dateTaken = ReturnStringIfNull(tiff[CGImageProperties.TIFFDateTime]);
                }
            }
        }
    }
}

}

小帮手功能

private string ReturnStringIfNull(NSObject inObj)
{
    if (inObj == null) return String.Empty;
    return inObj.ToString();
}
您可以从

引用 URL 请求PHAsset,该将包含一些元数据。您可以请求图像数据以获取更多信息。

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

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字符串,以避免令人讨厌的隐私崩溃

最新更新