Skip to content

PictureSelector 3.0 如何裁剪?

Luck edited this page Jan 14, 2022 · 11 revisions

更多请查阅uCrop

setImageEngine(); 图片加载引擎
setCropOutputPathDir(); 自定义裁剪输出路径
setCropOutputFileName(); 自定义裁剪输出文件名
isForbidSkipMultipleCrop(); 多图裁剪时是否支持跳过
isUseCustomLoaderBitmap(); 是否使用自定义加载裁剪资源
isCropDragSmoothToCenter(); 图片是否跟随裁剪框居中
isForbidCropGifWebp(); 是否禁止裁剪gif和webp

以UCrop裁剪为例:

.setCropEngine(new CropEngine() {
        @Override
        public void onStartCrop(Fragment fragment, LocalMedia currentLocalMedia, ArrayList<LocalMedia> dataSource, int requestCode) {
        // 注意* 如果你实现自己的裁剪库,需要在Activity的.setResult(); 
        // Intent中需要给MediaStore.EXTRA_OUTPUT,塞入裁剪后的路径;如果有额外数据也可以通过CustomIntentKey.EXTRA_CUSTOM_EXTRA_DATA字段存入;
        
        // 1、构造可用的裁剪数据源
        String currentCropPath = currentLocalMedia.getAvailablePath();
        Uri inputUri;
        if (PictureMimeType.isContent(currentCropPath) || PictureMimeType.isHasHttp(currentCropPath)) {
            inputUri = Uri.parse(currentCropPath);
        } else {
            inputUri = Uri.fromFile(new File(currentCropPath));
        }
        String fileName = DateUtils.getCreateFileName("CROP_") + ".jpg";
        Uri destinationUri = Uri.fromFile(new File(getSandboxPath(), fileName));
        ArrayList<String> dataCropSource = new ArrayList<>();
        for (int i = 0; i < dataSource.size(); i++) {
            LocalMedia media = dataSource.get(i);
            dataCropSource.add(media.getAvailablePath());
        }
        UCrop uCrop = UCrop.of(inputUri, destinationUri, dataCropSource);
        uCrop.setImageEngine(new UCropImageEngine() {
            @Override
            public void loadImage(Context context, String url, ImageView imageView) {
                Glide.with(context).load(url).into(imageView);
            }
        });
        uCrop.withOptions(buildOptions());
        uCrop.start(fragment.getActivity(), fragment, requestCode);                      
        }
	});