关于iPhone SDK:

我有一个512 x 512像素的.png图像,我希望在200 x 200像素的UIImageView中显示它与裁剪(原始分辨率,没有缩放,拟合或旋转)。有没有办法确定在UIImageView中可见的图像坐标?

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

我计划实施“泛”和“缩放”在某些方面起作用,但我想知道如何首先解决这个问题。 UIImageView类引用似乎没有帮助......我希望避免使用UIView,但如果有必要,我会去那条路。

谢谢!

有帮助吗?

解决方案 2

感谢您的帮助。在实现UIScrollView之后,解决平移/缩放功能变得微不足道。实现UIScrollView让我通过 myScrollView.bounds.origin.x myScrollView.bounds.origin.y 值确定我需要的坐标...缩放仍然需要计算 - 如上所述,但我现在正走在正确的轨道上。

这是我用来制作UIScrollView的代码(基于网络和本网站提供的多个来源 - Apple的 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];

其他提示

你对从UIImageView引用中看到的内容是对的 - 没有简单的方法可以单独使用UIImageView。您需要考虑UIImageView的contentMode之类的内容,或者仅限于计算单个UIViewContentMode的位置。

但是,当你最终实现平移和缩放时,你将使用带有UIImageView的UIScrollView作为该UIScrollView的子视图。在这一点上,最初将UIImageView的大小调整到你所显示的任何图像的大小(可能是动态的)是最明智的。然后,您可以通过确定图像视图的位置来确定图像的位置,这是您可以更简单地完成的,只需要一点点数学。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top