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.
Asked
Active
Viewed 406 times
2 Answers
2
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;
}

Matic Oblak
- 16,318
- 3
- 24
- 43
-
I am recieving memory warning.Actually Images are high resolution.If I dont zoom in , it gives memory warning after drawing 4 or 5 lines and then app crashes.but if image is zoomed in then its working fine ... why is it so ? – iOS_Learner Jan 28 '15 at 08:03
-
if its related to image size then why its not giving memory warning and crashing , when image is zoomed in ? – iOS_Learner Jan 28 '15 at 08:08
-
http://stackoverflow.com/questions/28207235/receiving-memory-warning-in-ipad-after-drawing-lines-on-large-size-image-with-cg# – iOS_Learner Jan 29 '15 at 09:09
-
http://stackoverflow.com/questions/28272062/app-crashes-due-to-memory-warning-on-ipad-3 – iOS_Learner Feb 02 '15 at 06:35
-
I think I have the correct solution for all modes below – Joel Teply Dec 09 '15 at 21:38
0
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);
}
}

Joel Teply
- 3,260
- 1
- 31
- 21