【问题标题】:Finding touch location according to image in UIImageView根据 UIImageView 中的图像查找触摸位置
【发布时间】:2015-01-27 09:16:51
【问题描述】:

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

【问题讨论】:

  • @MaticOblak 任何建议
  • @mclin 任何建议

标签: io uiimageview uiimage


【解决方案1】:

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

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

【讨论】:

  • 我收到内存警告。实际上图像是高分辨率的。如果我不放大,它会在绘制 4 或 5 行后给出内存警告,然后应用程序崩溃。但如果图像被放大然后它的工作好吧……为什么会这样?
  • 如果它与图像大小有关,那么为什么它在图像放大时不给出内存警告和崩溃?
  • 我认为我对以下所有模式都有正确的解决方案
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2012-07-15
    • 2022-01-25
    • 1970-01-01
    • 2018-01-11
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多