Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将点从图像转换为UIImageView点,支持contentMode_Iphone_Objective C_Ios_Cocoa Touch - Fatal编程技术网

Iphone 将点从图像转换为UIImageView点,支持contentMode

Iphone 将点从图像转换为UIImageView点,支持contentMode,iphone,objective-c,ios,cocoa-touch,Iphone,Objective C,Ios,Cocoa Touch,假设我有一个图像(例如1024x768px),它显示在UIImageView(例如300x300px)中。 现在,我想将图像中的一个点,例如人鼻子的位置(x:500,y:600)转换为UIImageView上的相应点,并考虑其内容模式 如果contentMode固定为UIViewContentModeScaleToFill,则转换将很容易。但如果是UIViewContentModeScaleAspectFit,事情就会变得更复杂 有没有一种优雅的方式来实现这一点?我真的不想对每一个content

假设我有一个图像(例如1024x768px),它显示在UIImageView(例如300x300px)中。 现在,我想将图像中的一个点,例如人鼻子的位置(x:500,y:600)转换为UIImageView上的相应点,并考虑其内容模式

如果contentMode固定为UIViewContentModeScaleToFill,则转换将很容易。但如果是UIViewContentModeScaleAspectFit,事情就会变得更复杂

有没有一种优雅的方式来实现这一点?我真的不想对每一个contentMode(我想超过10个)都进行计算


谢谢

不,没有内置的、公开的或优雅的方式来做这件事


您需要对自己需要的内容模式的功能进行反向工程。

为什么不能重新缩放坐标:

X(UIImageView)=(X(图像)/1024)*300


Y(UIImageView)=(Y(Image)/768)*300这是我的快速脏解决方案:

- (CGPoint)convertPoint:(CGPoint)sourcePoint fromContentSize:(CGSize)sourceSize {
    CGPoint targetPoint = sourcePoint;
    CGSize  targetSize  = self.bounds.size;

    CGFloat ratioX = targetSize.width / sourceSize.width;
    CGFloat ratioY = targetSize.height / sourceSize.height;

    if (self.contentMode == UIViewContentModeScaleToFill) {
        targetPoint.x *= ratioX;
        targetPoint.y *= ratioY;
    }
    else if(self.contentMode == UIViewContentModeScaleAspectFit) {
        CGFloat scale = MIN(ratioX, ratioY);

        targetPoint.x *= scale;
        targetPoint.y *= scale;

        targetPoint.x += (self.frame.size.width - sourceSize.width * scale) / 2.0f;
        targetPoint.y += (self.frame.size.height - sourceSize.height * scale) / 2.0f;
    }
    else if(self.contentMode == UIViewContentModeScaleAspectFill) {
        CGFloat scale = MAX(ratioX, ratioY);

        targetPoint.x *= scale;
        targetPoint.y *= scale;

        targetPoint.x += (self.frame.size.width - sourceSize.width * scale) / 2.0f;
        targetPoint.y += (self.frame.size.height - sourceSize.height * scale) / 2.0f;
    }

    return targetPoint;
}

- (CGRect)convertRect:(CGRect)sourceRect fromContentSize:(CGSize)sourceSize {
    CGRect targetRect = sourceRect;
    CGSize targetSize  = self.bounds.size;

    CGFloat ratioX = targetSize.width / sourceSize.width;
    CGFloat ratioY = targetSize.height / sourceSize.height;

    if (self.contentMode == UIViewContentModeScaleToFill) {
        targetRect.origin.x *= ratioX;
        targetRect.origin.y *= ratioY;

        targetRect.size.width *= ratioX;
        targetRect.size.height *= ratioY;
    }
    else if(self.contentMode == UIViewContentModeScaleAspectFit) {
        CGFloat scale = MIN(ratioX, ratioY);

        targetRect.origin.x *= scale;
        targetRect.origin.y *= scale;

        targetRect.origin.x += (self.frame.size.width - sourceSize.width * scale) / 2.0f;
        targetRect.origin.y += (self.frame.size.height - sourceSize.height * scale) / 2.0f;

        targetRect.size.width *= scale;
        targetRect.size.height *= scale;
    }
    else if(self.contentMode == UIViewContentModeScaleAspectFill) {
        CGFloat scale = MAX(ratioX, ratioY);

        targetRect.origin.x *= scale;
        targetRect.origin.y *= scale;

        targetRect.origin.x += (self.frame.size.width - sourceSize.width * scale) / 2.0f;
        targetRect.origin.y += (self.frame.size.height - sourceSize.height * scale) / 2.0f;

        targetRect.size.width *= scale;
        targetRect.size.height *= scale;
    }

    return targetRect;
}
当它被重构和优化后,我会将它发布在github上,并将链接发布到这里。
甚至这段代码也可能对某些人有所帮助。

今天,我有一些空闲时间来整理这个问题的解决方案,并将其发布在GitHub上:

它是UIImageView上的一个类别,提供了将点和矩形从图像转换为视图坐标的方法,并考虑了所有13种不同的内容模式


希望你喜欢

这对UIViewContentModeScaleToFill很好,但对于其他ContentMode?不适用于UIContentModeScaleToFill,因为将显示边距并且图像将在UIImageView中居中谢谢,我将对UIViewContentModeScaleToFill、UIViewContentModeAspectFit和UIViewContentModeAspectFill执行此操作。这应该足够了。@nubbel-完成后,请在github上或以其他方式公开,这样下一个人就不必跳过相同的障碍。我今天做了,请参阅我的其他帖子。我希望你能利用它!非常好-如果您也提供了其他方式(从imageView坐标到ImageCoordination坐标),那就更好了。不用麻烦了,我已经为一些模式完成了,我现在正在完成,今天晚些时候会在github上向您发送拉取请求。我发布了一个拉取请求:谢谢,我合并了您的更改!面对使用相机图像的旋转问题,库转换为不同的然后呈现的图像。