简体   繁体   English

如何确定UIImageView中较大的裁剪图像的图像坐标?

[英]How to determine image coordinates for a large cropped image within a UIImageView?

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). 我有一个512 x 512像素的.png图像,我想在200 x 200像素的UIImageView中进行裁剪显示(以原始分辨率,没有缩放,调整或旋转)。 Is there a way to determine the image's coordinates that are visible inside the UIImageView? 有没有一种方法可以确定在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. 我计划在某个时候实现“ pan”和“ zoom”功能,但我想知道如何首先解决此问题。 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. UIImageView类参考似乎没有帮助...我希望避免使用UIView,但是如果有必要,我会走这条路线。

Thanks! 谢谢!

You're right about what you've seen from the UIImageView reference - there is no easy way to do that using UIImageView alone. 您对从UIImageView参考中看到的内容是正确的-没有简单的方法可以单独使用UIImageView。 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的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. 但是,当最终实现平移和缩放时,将使用带有UIImageView的UIScrollView作为该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). 到那时,将UIImageView最初的大小调整为要显示的任何图像的大小(可以是动态的)是最明智的。 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. 然后,您可以通过确定图像视图的位置来确定图像的位置,这可以通过简单的数学操作就可以轻松完成。

Thanks for your help. 谢谢你的帮助。 Working out the pan/zoom features became trivial after implementing the UIScrollView. 在实现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,我可以使用myScrollView.bounds.origin.xmyScrollView.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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM