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

时间:2015-01-27 09:16:51

标签: io uiimageview uiimage

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

2 个答案:

答案 0 :(得分:1)

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

- (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;
}

答案 1 :(得分:0)

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

+ (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);
    }
}