简体   繁体   English

如何显示图像的唯一部分

[英]How to show the only part of the image

UPDATE Since the question was complicating and unclear, I'm rewriting my question to make it much simpler.更新由于问题复杂且不清楚,我正在重写我的问题以使其更简单。

在此处输入图像描述

Given给定

  • image (image uri for entire image; 600x600 image from the example)图像(整个图像的图像 uri;示例中的 600x600 图像)
  • left(x-coordinate; 50 from the example)左(x 坐标;示例中的 50)
  • top(y-coordinate; 100 from the example)顶部(y 坐标;示例中的 100)
  • width(width of the image; 300 from the example)宽度(图像的宽度;示例中为 300)
  • height (height of the image; 300 from the example)高度(图像的高度;示例中为 300)

what I want我想要的是

  • 300 x 300 image (which is cropped to the image) 300 x 300 图像(裁剪为图像)
  • 70 x 70 image (I will ultimately resized image to 70 x 70 size) 70 x 70 图像(我最终会将图像大小调整为 70 x 70 大小)

Here is my example code这是我的示例代码

// render the part of the image
console.log(left); // 50
console.log(thumbSize); // 300
return (
        <Image
          source={{uri: image}}
          style={selectedStyle(left, top, thumbSize)}/>
      );
... 
function selectedStyle(left, top, thumbSize) {
  return {
    left,
    top,
    width: thumbSize,
    height: thumbSize
  };
}

UPDATE from zvona's working demo, what I want is this.更新来自 zvona 的工作演示,我想要的是这个。

在此处输入图像描述 but nothing else.但没有别的。

在此处输入图像描述

Here is a working example: https://snack.expo.io/@zvona/cropped-image这是一个工作示例: https ://snack.expo.io/@zvona/cropped-image

The idea is to have "cropped" View where Image is positioned inside it with custom dimensions.这个想法是“裁剪” View ,其中Image位于其中并具有自定义尺寸。 I use constants in my example to clarify the case.我在示例中使用常量来说明情况。

<View style={styles.cropped}>
  <Image
    style={styles.image}
    source={{uri: 'https://upload.wikimedia.org/wikipedia/en/0/02/Homer_Simpson_2006.png'}} />
</View>

And on styles:关于样式:

  image: {
    marginLeft: -OFFSET_LEFT,
    marginTop: -OFFSET_TOP,
    width: IMAGE_WIDTH,
    height: IMAGE_HEIGHT,
  },

  cropped: {
    width: 150,
    height: 150,
    overflow: 'hidden',
    position: 'absolute',
    left: OFFSET_LEFT,
    top: OFFSET_TOP,
  },

Note that ImageBackground is only for example purposes and it's not needed in the actual implementation.请注意, ImageBackground仅用于示例目的,实际实现中不需要它。

Let me understand here.让我在这里了解一下。 You want to show only part of an image(x: 50, y:10, width: 300, height 300) and then resize this part to 70x70, right?您只想显示图像的一部分(x:50,y:10,宽度:300,高度 300),然后将此部分调整为 70x70,对吗? So 70x70 is the size of the final container.所以 70x70 是最终容器的大小。 First calculate the the x and y coords.首先计算 x 和 y 坐标。 with respect to the final size关于最终尺寸

var ar = 70 / 300
var x = 50 * ar
var y = 100 * ar

Then the width in percent:然后是百分比宽度:

var pc = (600 / 300) * 100

Assuming you show the cropped and resized image as a div background you do like this:假设您将裁剪和调整大小的图像显示为 div 背景,您可以这样做:

div.style.background = 'no-repeat ' + -x + 'px' + ' ' + -y + 'px/' + pc + '% url("' + imgAbsPath + '")'

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

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