2013-07-29 32 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

相關問題