Xamarin iOS相机和照片

时间:2018-03-09 22:39:34

标签: ios xamarin xamarin.ios camera photo

我正在使用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());

我能够启动相机,拍摄照片然后当我点击“使用照片”时... referenceURL返回为NULL ...如何获取网址,以便提取GPS坐标照片和其他属性?

2 个答案:

答案 0 :(得分:1)

我在 URL 上遇到了很多麻烦。它可以是一个文件,也可以是一个 web url,它在每个设备上的作用都不同。我的应用程序在我的测试组中崩溃并烧毁了很多次。我终于找到了一种从数据中获取元数据的方法。有多种方法可以获取 DateTaken、宽度和高度以及 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();
}

答案 1 :(得分:0)

您可以从参考网址请求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字符串,以避免令人讨厌的隐私崩溃