根据 UIImageView 中的图像查找触摸位置

Finding touch location according to image in UIImageView

我在 UIImageView 中有一个图像,UIImageView 的内容模式设置为 UIViewContentModeScaleAspectFit。那么如何根据图像找到触摸位置。请记住,我的图片尺寸有 2000 x 3000 这样的大分辨率。

试试这个代码。我没有测试它,所以可能会犯错误。希望评论能对您有所帮助,找到正确的解决方案。

- (CGPoint)point:(CGPoint)point onImageWithSize:(CGSize)imageSize inImageView:(UIImageView *)view contentMode:(UIViewContentMode)mode
{
    // find the relative image position on the view
    CGPoint imageRelativeOrigin = CGPointZero;
    CGSize imageRelativeSize = view.frame.size;

    if(mode == UIViewContentModeScaleAspectFit)
    {
        // we expect one of the origin coordinates has a positive offset
        // compare the ratio
        if(imageSize.width/imageSize.height > view.frame.size.width/view.frame.size.height)
        {
            // in this case the image width is the same as the view width but height is smaller
            imageRelativeSize = CGSizeMake(view.frame.size.width, view.frame.size.width*(imageSize.height/imageSize.width));
            CGFloat verticalOffset = (view.frame.size.height-imageRelativeSize.height)*.5f; // this is the visible image offset
            imageRelativeOrigin = CGPointMake(.0f, verticalOffset);
        }
        else
        {
            // in this case the image height is the same as the view height but widh is smaller
            imageRelativeSize = CGSizeMake(view.frame.size.height*(imageSize.width/imageSize.height), view.frame.size.height);
            CGFloat horizontalOffset = (view.frame.size.width-imageRelativeSize.width)*.5f; // this is the visible image offset
            imageRelativeOrigin = CGPointMake(horizontalOffset, .0f);
        }
    }
    else
    {
        // TODO: add other content modes
    }

    CGPoint relativeImagePoint = CGPointMake(point.x-imageRelativeOrigin.x, point.y-imageRelativeOrigin.y); // note these can now be off the image bounds
    // resize to image coordinate system
    CGPoint actualImagePoint = CGPointMake(relativeImagePoint.x*(imageSize.width/imageRelativeSize.width),
                                           relativeImagePoint.y*(imageSize.height/imageRelativeSize.height));
    return actualImagePoint;
}

这将处理所有内容模式。如果您需要知道它是否包含在您的图片中,只需将坐标与您的图片大小进行比较,当然如果它是正数。

+ (CGPoint) positionInImageView:(UIImageView *)imageView position:(CGPoint)position {
    float widthScale = imageView.image.size.width / imageView.bounds.size.width;
    float heightScale = imageView.image.size.height / imageView.bounds.size.height;

    int xOffset = 0;
    int yOffset = 0;

    if (imageView.contentMode == UIViewContentModeScaleToFill) {
        return CGPointMake(position.x * widthScale, position.y * heightScale);
    }
    else if (imageView.contentMode == UIViewContentModeScaleAspectFit || imageView.contentMode == UIViewContentModeScaleAspectFill) {

        float scale = 1.0;
        if ((widthScale > heightScale && imageView.contentMode == UIViewContentModeScaleAspectFit)
            || (widthScale < heightScale && imageView.contentMode == UIViewContentModeScaleAspectFill)) {
            scale = widthScale;
            yOffset = (imageView.image.size.height / heightScale - imageView.image.size.height / scale) / -2.0f;
        } else {
            scale = heightScale;
            xOffset = (imageView.image.size.width / widthScale - imageView.image.size.width / scale) / -2.0f;
        }
        return CGPointMake((position.x + xOffset) * scale,
                           (position.y + yOffset) * scale);
    } else {
        float widthDifference = imageView.image.size.width - imageView.bounds.size.width;
        float heightDifference = imageView.image.size.height - imageView.bounds.size.height;

        if (imageView.contentMode == UIViewContentModeTop
            || imageView.contentMode == UIViewContentModeBottom
            || imageView.contentMode == UIViewContentModeCenter){
            xOffset = widthDifference / 2.0f;
        } else if (imageView.contentMode == UIViewContentModeRight
                   || imageView.contentMode == UIViewContentModeTopRight
                   || imageView.contentMode == UIViewContentModeBottomRight){
            xOffset = widthDifference;
        }

        if (imageView.contentMode == UIViewContentModeCenter
            || imageView.contentMode == UIViewContentModeLeft
            || imageView.contentMode == UIViewContentModeRight){
            yOffset = heightDifference / 2.0f;
        } else if (imageView.contentMode == UIViewContentModeBottom
                   || imageView.contentMode == UIViewContentModeBottomLeft
                   || imageView.contentMode == UIViewContentModeBottomRight){
            yOffset = heightDifference;
        }

        return CGPointMake(position.x + xOffset, position.y + yOffset);
    }
}