Advertisement
Guest User

XF Custom Renderer for ImageCropper android library

a guest
Aug 14th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. public class ImagePickerService : IImagePickerService
  2.     {
  3.         public IImageSourceUtility ImageSourceUtility => new ImageSourceUtility();
  4.  
  5.         private void StartActivity()
  6.         {
  7.             var currentActivity = MainActivity.AppActivity;
  8.  
  9.             if (currentActivity != null)
  10.             {
  11.                 var cropImageOptions = new CropImageOptions();
  12.                 cropImageOptions.MultiTouchEnabled = true;
  13.                 cropImageOptions.Guidelines = CropImageView.Guidelines.On;
  14.                 cropImageOptions.AspectRatioX = 1;
  15.                 cropImageOptions.AspectRatioY = 1;
  16.                 cropImageOptions.FixAspectRatio = true;
  17.                 cropImageOptions.Validate();
  18.                 var intent = new Intent();
  19.                 intent.SetClass(currentActivity, Class.FromType(typeof(ImagePickerOnResultActivity)));
  20.                 intent.PutExtra(CropImage.CropImageExtraSource, null as global::Android.Net.Uri); // Image Uri
  21.                 intent.PutExtra(CropImage.CropImageExtraOptions, cropImageOptions);
  22.                 currentActivity.StartActivity(intent);
  23.             }
  24.             else
  25.             {
  26.                 throw new InvalidOperationException("Could not get current activity.");
  27.             }
  28.         }
  29.  
  30.         public Task<ImageSource> PickImageAsync()
  31.         {
  32.             StartActivity();
  33.  
  34.             return Task.Run(() =>
  35.             {
  36.                 _waitHandle.WaitOne();
  37.                 var result = _pickAsyncResult;
  38.                 _pickAsyncResult = null;
  39.  
  40.                 return result;
  41.             });
  42.         }
  43.  
  44.         private static ImageSource _pickAsyncResult;
  45.         private static EventWaitHandle _waitHandle = new AutoResetEvent(false);
  46.  
  47.         // if crashes(in release mode after linking), see plugin desc for proguard config change required for this theme
  48.         [Activity(Label = "CropImageActivity", Theme = "@style/Base.Theme.AppCompat")]
  49.         //[Activity(Theme = "@style/Base.Theme.AppCompat")]
  50.         public class ImagePickerOnResultActivity : Activity
  51.         {
  52.             protected override void OnCreate(Bundle savedInstanceState)
  53.             {
  54.                 //base.OnCreate(savedInstanceState);
  55.  
  56.                 // start activity
  57.                 var x = CropImage.Activity();
  58.                     x.SetGuidelines(CropImageView.Guidelines.On)
  59.                     .Start(this);
  60.             }
  61.  
  62.             protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
  63.             {
  64.                 if (requestCode == CropImage.CropImageActivityRequestCode)
  65.                 {
  66.                     CropImage.ActivityResult result = CropImage.GetActivityResult(data);
  67.                     if (resultCode == Result.Ok)
  68.                     {
  69.                         var croppedFileUri = new Uri(result.Uri.ToString());
  70.                         _pickAsyncResult = ImageSource.FromFile(croppedFileUri.LocalPath);
  71.                         _waitHandle.Set();
  72.                     }
  73.                     else if ((int)resultCode == CropImage.CropImageActivityResultErrorCode)
  74.                     {
  75.                         Java.Lang.Exception error = result.Error;
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement