Finding touch location according to image in UIImageView

夙愿已清 提交于 2019-12-11 13:11:33

问题


I have an image in UIImageView and the content mode of UIImageView is set be UIViewContentModeScaleAspectFit. so how can I find the touch location according to image. Keep it in mind , my image size has large resolution like 2000 x 3000.


回答1:


Try this code. I did not test it so mistakes might have been made. I hope the comments will help you enough to find the correct solution.

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



回答2:


This will handle all content modes. If you need to know whether it is contained within your image, just compare the coordinates to the size of your image and of course if it is positive.

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


来源:https://stackoverflow.com/questions/28166819/finding-touch-location-according-to-image-in-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!