UIImagePickerControllerReferenceURL always returns nill

后端 未结 3 1649
孤城傲影
孤城傲影 2021-01-05 13:02

I am trying to get the name of the image which I have just captured from camera with following code. But [info objectForKey:@\"UIImagePickerControllerReferenceURL\"]

3条回答
  •  太阳男子
    2021-01-05 13:34

    For Xamarin.iOS developers: store image capture from camera and get its data using ALAssetsLibrary

    var originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
    var meta = e.Info[UIImagePickerController.MediaMetadata] as NSDictionary;
    
    //Get image bytes 
    if (originalImage != null) 
    {
        using (NSData imageData = originalImage.AsPNG())
        {
            myByteArray = new Byte[imageData.Length];
            System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length));
        }
    
        //This bit of code saves image to the Photo Album with metadata
        ALAssetsLibrary library = new ALAssetsLibrary();
        library.WriteImageToSavedPhotosAlbum(originalImage.CGImage, meta, (assetUrl, error) =>
        {
            library.AssetForUrl(assetUrl, delegate (ALAsset asset)
            {
                ALAssetRepresentation representation = asset.DefaultRepresentation;
                if (representation != null)
                {
                    string fileName = representation.Filename;
                    var filePath = assetUrl.ToString();
                    var extension = filePath.Split('.')[1].ToLower();
                    var mimeData = string.Format("image/{0}", extension);
                    var mimeType = mimeData.Split('?')[0].ToLower();
                    var documentName = assetUrl.Path.ToString().Split('/')[1];
                }
            }, delegate (NSError err) {
                Console.WriteLine("User denied access to photo Library... {0}", err);
            });
        });
    }
    

提交回复
热议问题