首先,添加ImageCropper.Forms.Fix.v2nuget 包到您的项目中。
那么,如果你使用AndroidX
,请更改AndroidManifest.xaml标签中的代码
<application android:label="ImageCropDemo.Android" android:theme="@style/MainTheme">
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat"/>
<provider android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
另外,您不需要调用CrossMedia.Current.PickPhotoAsync,只需像下面的代码一样直接执行ImageCropper即可。
namespace ImageCropDemo
{
public partial class MainPage : ContentPage
{
private MediaFile _mediaFile;
public MainPage()
{
InitializeComponent();
CrossMedia.Current.Initialize();
}
protected void OnClickedRectangle(object sender, EventArgs e)
{
new ImageCropper()
{
// PageTitle = "Test Title",
// AspectRatioX = 1,
// AspectRatioY = 1,
Success = (imageFile) =>
{
Device.BeginInvokeOnMainThread(() =>
{
image.Source = ImageSource.FromFile(imageFile);
});
}
}.Show(this);
}
async void OpenCamera(object sender, EventArgs args)
{
try
{
await CrossMedia.Current.Initialize();
//if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
//{
// await DisplayAlert("Alert", "No camera available.", "Ok");
// return;
//}
//_mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
//{
// Directory = "Sample",
// Name = "test.jpg",
// AllowCropping = true,
// PhotoSize = PhotoSize.Medium
//});
//if (_mediaFile == null)
// return;
//image.Source = ImageSource.FromStream(() =>
//{
// if (Device.OS == TargetPlatform.iOS)
// {
// return _mediaFile.GetStreamWithImageRotatedForExternalStorage();
// }
// else
// {
// return _mediaFile.GetStream();
// }
//});
new ImageCropper()
{
PageTitle = "Test Title",
AspectRatioX = 1,
AspectRatioY = 1,
CropShape = ImageCropper.CropShapeType.Rectangle,
SelectSourceTitle = "Select source",
TakePhotoTitle = "Take Photo",
PhotoLibraryTitle = "Photo Library",
Success = (imageFile) =>
{
Device.BeginInvokeOnMainThread(() =>
{
image.Source = ImageSource.FromFile(imageFile);
});
}
}.Show(this);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("CameraException:>" + ex);
}
}
string imagefile;
async void OpenGallery(object sender, EventArgs args)
{
try
{
await CrossMedia.Current.Initialize();
//if (!CrossMedia.Current.IsPickPhotoSupported)
//{
// await DisplayAlert("Alert", "No photos available.", "Ok");
// return;
//}
//_mediaFile = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions
//{
// PhotoSize = PhotoSize.Medium
//});
//if (_mediaFile == null)
// return;
//image.Source = ImageSource.FromStream(() =>
//{
// return _mediaFile.GetStream();
//});
new ImageCropper()
{
PageTitle = "Test Title",
AspectRatioX = 1,
AspectRatioY = 1,
CropShape = ImageCropper.CropShapeType.Rectangle,
SelectSourceTitle = "Select source",
TakePhotoTitle = "Take Photo",
PhotoLibraryTitle = "Photo Library",
Success = (imageFile) =>
{
Device.BeginInvokeOnMainThread (() =>
{
image.Source = ImageSource.FromFile(imageFile);
imagefile = imageFile;
});
}
}.Show(this);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("GalleryException:>" + ex);
}
}
}
}