如何在UIImageView中为一个大裁剪的图像确定图像坐标?

时间:2023-02-09 10:03:07

With regards to the iPhone SDK:

关于iPhone SDK:

I have a 512 x 512 pixel .png image, and I want to display it within a 200 x 200 pixel UIImageView WITH cropping (at native resolution, without zooming, fitting, or rotation). Is there a way to determine the image's coordinates that are visible inside the UIImageView?

我有一个512 x 512像素的。有办法确定在UIImageView中可见的图像坐标吗?

For example, if the image in the exact center of the UIImageView, then I want to know what the coordinates of the image are at UIImageView's (0,0) and (200,200) points, which will be (156,156) and (356,356), respectively for the image being displayed.

例如,如果图像位于UIImageView的正中心,那么我想知道图像在UIImageView(0,0)和(200,200)点上的坐标是什么,这两个点分别是(156,156)和(356,356)。

I plan on implementing "pan" and "zoom" functions at some point, but I want to know how to address this issue first. The UIImageView class reference did not seem helpful... I was hoping to avoid using a UIView, but if that is necessary I will go that route.

我计划在某个时候实现“pan”和“zoom”功能,但是我想知道如何首先解决这个问题。UIImageView类引用似乎没有帮助…我希望避免使用UIView,但如果有必要的话,我将走那条路。

Thanks!

谢谢!

2 个解决方案

#1


0  

You're right about what you've seen from the UIImageView reference - there is no easy way to do that using UIImageView alone. You would need to take into account things like the contentMode of the UIImageView or limit yourself to only calculating the position for a single UIViewContentMode.

你从UIImageView引用中看到的内容是正确的-使用UIImageView是不容易做到的。你需要考虑像UIImageView的contentMode之类的东西或者限制自己只计算单个UIViewContentMode的位置。

However, when you eventually get to implementing pan and zoom, you'll be using a UIScrollView with a UIImageView as a subview of that UIScrollView. At that point, it would be most sane to size your UIImageView initially to the size of whatever image you're display (could be dynamic). You can then determine where the image is by determining where the image view is, which is something you can accomplish much more simply and with just a bit of math.

但是,当您最终实现pan和zoom时,您将使用带有UIImageView的UIScrollView作为UIScrollView的子视图。此时,将UIImageView的大小设置为显示的任何图像的大小(可能是动态的)是最合理的。然后,您可以通过确定映像视图的位置来确定映像的位置,这是您可以更简单地完成的事情,只需一点点数学运算。

#2


0  

Thanks for your help. Working out the pan/zoom features became trivial after implementing the UIScrollView. Implementing the UIScrollView let me determine the coordinates I needed with the myScrollView.bounds.origin.x and myScrollView.bounds.origin.y values... zooming still requires calculation - as you stated above, but I am on the right track now.

谢谢你的帮助。在实现了UIScrollView后,开发pan/zoom特性变得非常简单。实现UIScrollView让我用myScrollView.bounds.origin确定需要的坐标。x和myScrollView.bounds.origin。y值……缩放仍然需要计算——正如你上面所说的,但是我现在正在正确的轨道上。

here is the code I used to make the UIScrollView (based on multiple sources available on the net and this site - Apple's ScrollViewSuite was particularly helpful. Here is the code I used in the (void)viewDidLoad section of my program, hopefully others can find it useful:

下面是我用来制作UIScrollView的代码(基于网络和本网站上的多种资源),苹果的ScrollViewSuite非常有用。下面是我在程序(void)viewDidLoad部分中使用的代码,希望其他人能发现它有用:

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[imageArray objectAtIndex:0]]; //imageArray is an array/stack of images...
[self setMyImage:tempImageView]; //myImage is of type UIImageView
[myImage setTag:100]; //set this to an 'arbitrary' value so that when I update the scrollview I can remove the current image...
[tempImageView release];

myScrollView.contentSize = CGSizeMake(myImage.frame.size.width, myImage.frame.size.height);
myScrollView.maximumZoomScale = 10.0;
myScrollView.minimumZoomScale = 0.15;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;

[myScrollView addSubview:myImage];

#1


0  

You're right about what you've seen from the UIImageView reference - there is no easy way to do that using UIImageView alone. You would need to take into account things like the contentMode of the UIImageView or limit yourself to only calculating the position for a single UIViewContentMode.

你从UIImageView引用中看到的内容是正确的-使用UIImageView是不容易做到的。你需要考虑像UIImageView的contentMode之类的东西或者限制自己只计算单个UIViewContentMode的位置。

However, when you eventually get to implementing pan and zoom, you'll be using a UIScrollView with a UIImageView as a subview of that UIScrollView. At that point, it would be most sane to size your UIImageView initially to the size of whatever image you're display (could be dynamic). You can then determine where the image is by determining where the image view is, which is something you can accomplish much more simply and with just a bit of math.

但是,当您最终实现pan和zoom时,您将使用带有UIImageView的UIScrollView作为UIScrollView的子视图。此时,将UIImageView的大小设置为显示的任何图像的大小(可能是动态的)是最合理的。然后,您可以通过确定映像视图的位置来确定映像的位置,这是您可以更简单地完成的事情,只需一点点数学运算。

#2


0  

Thanks for your help. Working out the pan/zoom features became trivial after implementing the UIScrollView. Implementing the UIScrollView let me determine the coordinates I needed with the myScrollView.bounds.origin.x and myScrollView.bounds.origin.y values... zooming still requires calculation - as you stated above, but I am on the right track now.

谢谢你的帮助。在实现了UIScrollView后,开发pan/zoom特性变得非常简单。实现UIScrollView让我用myScrollView.bounds.origin确定需要的坐标。x和myScrollView.bounds.origin。y值……缩放仍然需要计算——正如你上面所说的,但是我现在正在正确的轨道上。

here is the code I used to make the UIScrollView (based on multiple sources available on the net and this site - Apple's ScrollViewSuite was particularly helpful. Here is the code I used in the (void)viewDidLoad section of my program, hopefully others can find it useful:

下面是我用来制作UIScrollView的代码(基于网络和本网站上的多种资源),苹果的ScrollViewSuite非常有用。下面是我在程序(void)viewDidLoad部分中使用的代码,希望其他人能发现它有用:

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[imageArray objectAtIndex:0]]; //imageArray is an array/stack of images...
[self setMyImage:tempImageView]; //myImage is of type UIImageView
[myImage setTag:100]; //set this to an 'arbitrary' value so that when I update the scrollview I can remove the current image...
[tempImageView release];

myScrollView.contentSize = CGSizeMake(myImage.frame.size.width, myImage.frame.size.height);
myScrollView.maximumZoomScale = 10.0;
myScrollView.minimumZoomScale = 0.15;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;

[myScrollView addSubview:myImage];