2013-07-29 58 views
0

我正在一个视图控制器,其中包含两个UIImageView s。底部图像视图保存照片(由用户拍摄或选择)。第二个图像视图位于此之上,占据了整个屏幕。使用点和多个图像视图

第二个图像视图包含一个指针(下例中为绿点)。它用作可移动视图,可以在屏幕上定位。它的用途是在背后的照片上标记一个位置/点。

在肖像这工作正常,底部图像视图设置为图像=方面填充。因此拍摄的照片占据整个屏幕。

在横向方向,这不起作用。我可以在下面

enter image description here

解释与

严重绘制图像更好一点作为一个例子,如果用户选择在纵向视图的点(由绿点示出)大概位置是220,380。

但是,如果同一位置上的风景会不会对后面的照片相同的位置。

该点将转化为更多像280,300。

所以,问题是......当VC是定向的风景,我怎么确定(从底部的图像视图中),图像的高度和宽度,制定出的相同点?

或者是否有另一种方法/方法来完成此?

-------编辑---------- 我已经创建了一个测试应用程序,只有这个功能。我具有与上面详述的相同的设置。我已将日志添加到视图,并将方向更改为横向。下面是我使用的日志:

NSLog(@"bottom image view width: %f",self.photoImageView.frame.size.width); 
NSLog(@"bottom image view height: %f",self.photoImageView.frame.size.height); 
NSLog(@"bottom image view frame bounds X: %f",self.photoImageView.frame.origin.x); 
NSLog(@"bottom image view frame bounds Y: %f",self.photoImageView.frame.origin.y); 
NSLog(@"bottom image view bounds X: %f",self.photoImageView.bounds.origin.x); 
NSLog(@"bottom image view bounds Y: %f",self.photoImageView.bounds.origin.y); 
NSLog(@"---------BOTTOM IMAGE VIEW IMAGE DETAILS---------"); 
NSLog(@"bottom image view image width: %f",self.photoImageView.image.size.width); 
NSLog(@"bototm image view image height: %f",self.photoImageView.image.size.height); 
NSLog(@"bottom image view image frame bounds X: %f",self.photoImageView.image.accessibilityFrame.origin.x); 
NSLog(@"buttom image view image frame bounds Y: %f",self.photoImageView.image.accessibilityFrame.origin.y); 

这些结果如下:

2013-07-30 14:58:23.013 SelectPhotoViewTest[3414:c07] ---------VIEW DETAILS--------- 
2013-07-30 14:58:23.014 SelectPhotoViewTest[3414:c07] ---------BOTTOM IMAGE VIEW DETAILS--------- 
2013-07-30 14:58:23.015 SelectPhotoViewTest[3414:c07] bottom image view width: 480.000000 
2013-07-30 14:58:23.016 SelectPhotoViewTest[3414:c07] bottom image view height: 268.000000 
2013-07-30 14:58:23.016 SelectPhotoViewTest[3414:c07] bottom image view frame bounds X: 0.000000 
2013-07-30 14:58:23.017 SelectPhotoViewTest[3414:c07] bottom image view frame bounds Y: 0.000000 
2013-07-30 14:58:23.018 SelectPhotoViewTest[3414:c07] bottom image view bounds X: 0.000000 
2013-07-30 14:58:23.018 SelectPhotoViewTest[3414:c07] bottom image view bounds Y: 0.000000 
2013-07-30 14:58:23.019 SelectPhotoViewTest[3414:c07] ---------BOTTOM IMAGE VIEW IMAGE DETAILS--------- 
2013-07-30 14:58:23.019 SelectPhotoViewTest[3414:c07] bottom image view image width: 258.000000 
2013-07-30 14:58:23.019 SelectPhotoViewTest[3414:c07] bototm image view image height: 480.000000 
2013-07-30 14:58:23.020 SelectPhotoViewTest[3414:c07] bottom image view image frame bounds X: 0.000000 
2013-07-30 14:58:23.021 SelectPhotoViewTest[3414:c07] buttom image view image frame bounds Y: 0.000000 

如何从这些结果中得到,以确定中心点是相对于底部图像坐标?

回答

0

实际上,您必须在两个方向上将点转换为图像的坐标系。在您给出的示例中,仅适用于肖像,因为图像的宽高比与屏幕相同。如果你有一个正方形的图像,该点与图像没有1:1的关系,并且可以很容易地放置在图像之外。

我能想到的两种方法来实现自己的目标,把我的头顶部:

1)代替具有底部的ImageView占据整个屏幕,并拥有它一方面填充,手动调整它占用或者整个屏幕的宽度或高度,取决于图像。即如果您有1000x1000的图片,则您希望图片视图的portait(iphone3)为320x320或横向为300x300。然后,将第二张图像作为第一张的子视图。它的位置现在将相对于底部图像的坐标系。

2)使用一些简单的数学运算将屏幕上的点转换成图像的坐标系。您知道底部图像视图的大小和纵横比,并且您对图像也了解相同。

假设图像是640x920。在风景中,底部图像视图将为480x300。

1)缩放图像以大小它会在图像视图(209x300)

2)由于图像将居中,它会从左侧

3)左右开始136点因此屏幕上的点(280,300)将相对于较小图像转换为(144,280 [状态栏的-20点]),在整个图像的坐标系中转换为(441,859)。

我希望这可以指引您正确的方向。

编辑:

好了,你的例子日志的工作:

[顺便说一句,你可以更容易地这样打印出的CGRect:的NSLog(@ “%@”,NSStringFromCGRect(自我。 photoImageView.frame))]

1)缩放图像尺寸,从而它会装配到图像视图:

ratio = photoImageView.frame.size.height/photoImageView.image.size.height; 
height = photoImageView.image.size.height * ratio; 
width = photoImageView.image.size.width * ratio; 

2)计算的F假设使用图像视图

x = (photoImageView.frame.size.width - width)/2; 
y = 0; 
frame = CGRectMake(x, y, width, height); 

3)内的图像的RAME [触摸locationInView:photoImageView]得到位置点,可以检查触摸是否在图像帧中与

CGRectContainsPoint(frame, location) 

编辑 - 包括使用的实际代码 - 与上面处理图像视图的方向略有不同。

if (self.photoImageView.frame.size.height < self.photoImageView.frame.size.width) { 
    // This is device and image view is landscape 
    if (self.pushedPhoto.size.height < self.pushedPhoto.size.width) { 
     // The pushed photo is portrait 
     self.photoImageViewRatio = self.photoImageView.frame.size.height/self.pushedPhoto.size.height; 
    } else { 
     // The pushed photo is landscape 
     self.photoImageViewRatio = self.photoImageView.frame.size.width/self.pushedPhoto.size.width; 
    } 

} else { 
    // This is device and image view is portrait 
    if (self.pushedPhoto.size.height < self.pushedPhoto.size.width) { 
     // The pushed photo is portrait 
     self.photoImageViewRatio = self.photoImageView.frame.size.width/self.pushedPhoto.size.width; 

    } else { 
     // The pushed photo is landscape 
     self.photoImageViewRatio = self.photoImageView.frame.size.height/self.pushedPhoto.size.height; 
    } 
} 

self.valueHeight = self.pushedPhoto.size.height * self.photoImageViewRatio; 
self.valueWidth = self.pushedPhoto.size.width * self.photoImageViewRatio; 




float x = (self.photoImageView.frame.size.width - self.valueWidth)/2; 
float y = (self.photoImageView.frame.size.height - self.valueHeight) /2; 
+0

谢谢,我认为第二个选项将是最好的实施。但是,两者都可以工作。我选择第二个的原因是因为顶部图像视图使用了过大的图像。这是为了说明imageview的移动,并确保视图总是在屏幕上,当完全移动到一侧时。然而,我不确定你在答案中是如何计算出这些例子的,你能否详细说明一下?我不确定如何从顶部图像视图的中心点到底部图像的图像坐标以及使用的方法。 – StuartM

+0

顶部图像的中心点与整个屏幕还是自己的超级视图有关? – robinkunde

+0

这将是在屏幕上的uiimageview的中心点 – StuartM

相关问题