简体   繁体   English

将多图像选择器与图像裁剪器一起使用

[英]Use Multi Image Picker with Image Cropper

I am using the Multi_Image_Picker plugin to get multiple images.我正在使用 Multi_Image_Picker 插件来获取多个图像。 Multi_Image_Picker returns a List<Asset> files when selecting multiple images. Multi_Image_Picker 在选择多个图像时返回一个List<Asset>文件。 How would I be able to use Multi_Image_Picker along with Image_Cropper which only accepts the path to the image?我如何能够将 Multi_Image_Picker 与只接受图像路径的 Image_Cropper 一起使用? I couldn't get the path of the image since its an Asset type.由于它是资产类型,因此我无法获取图像的路径。 Here is what I've tried in order to achieve it:这是我为实现它所做的尝试:

I could get the path of the image:我可以得到图像的路径:

final filePath = await FlutterAbsolutePath.getAbsolutePath(assets.identifier);

This works but then flutter_absolute_path plugin requires the minimum android sdk to be 19. Is there another to crop images without converting the Asset File into an Image File?这可行,但是flutter_absolute_path插件需要最小的android sdk为19。是否有另一个可以在不将资产文件转换为图像文件的情况下裁剪图像?

I tried converting the Asset to Image File:我尝试将资产转换为图像文件:

List<File> images = List<File>();
Directory tempDir = await getTemporaryDirectory();
final path =tempDir.path;
for (int i = 0; i < assets.length; i++) {
   images.add(await ProcessImage.assetToFile(
     path: "$path/images/img$i",
      data: await assets[i].getByteData(quality: 90)));
}

assetToFile():资产文件():

static Future<File> assetToFile({ByteData data, String path})async {
return File(path).writeAsBytes(
      data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}

Thanks to @pskink for the answer.感谢@pskink 的回答。 It turns out that you have to save the obtained asset byte data to a temporary folder as a file and use it in your cropper.事实证明,您必须将获得的资产字节数据作为文件保存到临时文件夹中,并在您的裁剪器中使用它。

final temp = await Directory.systemTemp.createTemp();
List<File> images = List<File>();
 for (int i = 0; i < assets.length; i++) {
    final data = await assets[i].getByteData();
    images.add(await File('${temp.path}/img$i').writeAsBytes(
      data.buffer.asUint8List(
        data.offsetInBytes, data.lengthInBytes)));
}

You can find a good example of a combination of three packages in below Github repository:您可以在下面的 Github 存储库中找到三个包组合的一个很好的示例:

  • multi_image_picker multi_image_picker
  • image_cropper image_cropper
  • photofilters滤光片

https://github.com/flutterstudygn/multiple_image_selector https://github.com/flutterstudygn/multiple_image_selector

Key features主要特征

  • Pick multiple images.选择多个图像。
  • Take a picture in the grid view.在网格视图中拍照。
  • Restrict the maximum count of images the user can pick.限制用户可以选择的最大图像数量。
  • Adjust cropping for each image.调整每个图像的裁剪。
  • Adjust filter for each image.调整每个图像的过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM