簡體   English   中英

如何在React Native中裁剪圖像

[英]How to crop image in React Native

我正在使用react-native-camera拍照。 拍攝的照片是16/9比例,但我需要它們在4/3。

因此我想要的是將圖像裁剪為例如1920*1440

我使用React Native的ImageEditor ImageEditor的代碼可以在這里找到。

我的代碼如下:

this.camera.capture(target)
     .then((data) => {
           let cropData = {
               offset:{x:0,y:0},
               size:{width:1920, height:1440}
           };

           ImageEditor.cropImage(
                 data.path,
                 cropData, 
                 (uri) => {
                       this.props.captured(this.props.request, {
                             path: uri,
                             data: data.data,
                             longitude: position.coords.longitude,
                             latitude: position.coords.latitude
                             }
                       );
                       Actions.pop();
                 },
                 (error) => {});
            })
     .catch(err => {
           console.error(err)
     });

但上面的代碼不起作用。 保存的照片未裁剪,為1440*2560

有什么建議?

現在還不完全清楚你的代碼在this.props.captured captured()會發生什么,但我認為問題在於你將原始data傳遞給this.props.captured 我從文檔中得到的理解是:

如果裁剪過程成功,則生成的裁剪圖像將存儲在ImageStore中,並且success回調中返回的URI將指向存儲中的圖像。

因此,您應該從uri讀取裁剪后的圖像,而不是重復使用data

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM