【问题标题】:Converting UIImageView Touch Coordinates to UIImage Coordinates Aspect Fill将 UIImageView Touch Coordinates 转换为 UIImage Coordinates Aspect Fill
【发布时间】:2018-04-01 11:12:40
【问题描述】:

我已经在我的应用中实现了这段代码

Converting UIImageView Touch Coordinates to UIImage Coordinates

这段代码就像一个魅力,但我正在尝试做同样的事情,但使用 Aspect Fill,据我了解,de 代码将不起作用,因为如果图像更小会缩放更大,所以我看不到部分在宽高比与图像不同的情况下,图像的大小。

这是我正在实现的代码:

private func pointConversion(point: CGPoint) -> CGPoint {

    if let image = self.imageView.image {
        let percentX = point.x / self.imageView.frame.size.width
        let percentY = point.y / self.imageView.frame.size.height
        return CGPoint(x: image.size.width * percentX , y: image.size.height * percentY)
    }
    return CGPoint(x: 0, y: 0)
}

提前致谢。

【问题讨论】:

    标签: ios iphone swift uiimageview uiimage


    【解决方案1】:

    在填充视图的图像至少在一个维度上小于视图的情况下,以下内容适用于我。不确定是否遇到过两个图像尺寸都大于目标视图的测试:

    func mapPointThroughAspectFill(uiViewPoint:CGPoint)->CGPoint
    // Maps a point from the coordinate space of our UIView into the space of the image
    // which AspectFills that UIView
    {
        let imageWidth = imageView!.image!.size.width
        let imageHeight = imageView!.image!.size.height
        let xScale = imageView!.bounds.size.width / imageWidth
        let yScale = imageView!.bounds.size.height / imageHeight
        var x, y,  o:CGFloat
    
        if (xScale>yScale) {
            // scale vertically from the center for height-pegged images
            o = (imageView.bounds.size.height - (imageHeight*xScale))/2;
            x = uiViewPoint.x / xScale;
            y = (uiViewPoint.y-o) / xScale;
        } else {
            // scale horizontally from the center for width-pegged images
            o = (imageView.bounds.size.width - (imageWidth*yScale))/2;
            x = (uiViewPoint.x-o) / yScale;
            y = uiViewPoint.y / yScale;
        }
        return CGPoint(x: x, y: y)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-07
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 2015-07-29
      相关资源
      最近更新 更多