2011-08-27 3 views
15

UIImageView에 표시되는 이미지 (예 : 1024 x 768 픽셀)가 있다고 가정 해 봅니다 (예 : 300 x 300 픽셀). 이제 이미지에서 한 점을 변환하고 싶습니다. 사용자의 코 (x : 500, y : 600)의 위치를 ​​contentMode가 고려 된 UIImageView의 해당 지점으로 매핑합니다.이미지를 UIImageView 포인트로 변환합니다. 콘텐츠 모드를 인식합니다.

contentMode가 UIViewContentModeScaleToFill로 고정되어 있으면 쉽게 변환 할 수 있습니다. 그러나 그것이 UIViewContentModeScaleAspectFit 일 경우 더욱 복잡해집니다.

우아한 방법이 있습니까? 나는 모든 contentMode (10 개 이상, 나는 생각한다)에 대해 그것을 계산하고 싶지는 않다.

감사합니다.

답변

29

오늘은이 문제에 함께 내 솔루션을 넣고 GitHub의에 게시하는 몇 가지 여분의 시간을 가졌다 : UIImageView-GeometryConversion

이보기 이미지에서 점의 구형을 변환하는 방법을 제공한다있는 UIImageView에 대한 범주입니다 13 가지 다른 모든 콘텐츠 모드를 조정하고 존중합니다.

희망 하시길 바랍니다.

+0

Very nice - 이미지 뷰 좌표에서 이미지 좌표까지 다른 방법으로 제공했다면 더 좋을 것입니다. – sarfata

+0

네 말이 맞아, 곧 추가 할게. – nubbel

+1

귀찮게하지 말고, 몇 가지 모드로 해 봤는데, 지금 당장 끝내고 있습니다. 오늘 나중에 github에 풀 요청을 보낼 것입니다. – sarfata

2

아니요, 내장, 공개 또는 우아한 방법이 없습니다.

자신에게 필요한 콘텐츠 모드의 기능을 리버스 엔지니어링해야합니다.

+0

감사합니다. UIViewContentModeScaleToFill, UIViewContentModeAspectFit 및 UIViewContentModeAspectFill에 대해 수행 할 예정입니다. 그 정도면 충분합니다. – nubbel

+2

@nubbel - 완료되면 github 또는 다른 방법으로 공개하여 다음 사람이 동일한 농구를 뛰어 넘을 필요가 없도록하십시오. – PeyloW

-1

왜 그냥 좌표를 재조정하지 못할 :

X (있는 UIImageView) = (/ 1,024 X (이미지)) * 300

Y (있는 UIImageView) = (Y (이미지)/768) * 내 quick'n'dirty 솔루션입니다 (300)

+0

UIViewContentModeScaleToFill에는 좋지만 다른 contentMode에는 적합합니까? UIContentModeScaleToFit에 대해 – nubbel

+0

이 작동하지 않습니다. 여백이 표시되고 이미지가 uiimageview의 중앙에 위치하기 때문에 – Vassily

10

:

이 리팩토링 것
- (CGPoint)convertPoint:(CGPoint)sourcePoint fromContentSize:(CGSize)sourceSize { 
    CGPoint targetPoint = sourcePoint; 
    CGSize targetSize = self.bounds.size; 

    CGFloat ratioX = targetSize.width/sourceSize.width; 
    CGFloat ratioY = targetSize.height/sourceSize.height; 

    if (self.contentMode == UIViewContentModeScaleToFill) { 
     targetPoint.x *= ratioX; 
     targetPoint.y *= ratioY; 
    } 
    else if(self.contentMode == UIViewContentModeScaleAspectFit) { 
     CGFloat scale = MIN(ratioX, ratioY); 

     targetPoint.x *= scale; 
     targetPoint.y *= scale; 

     targetPoint.x += (self.frame.size.width - sourceSize.width * scale)/2.0f; 
     targetPoint.y += (self.frame.size.height - sourceSize.height * scale)/2.0f; 
    } 
    else if(self.contentMode == UIViewContentModeScaleAspectFill) { 
     CGFloat scale = MAX(ratioX, ratioY); 

     targetPoint.x *= scale; 
     targetPoint.y *= scale; 

     targetPoint.x += (self.frame.size.width - sourceSize.width * scale)/2.0f; 
     targetPoint.y += (self.frame.size.height - sourceSize.height * scale)/2.0f; 
    } 

    return targetPoint; 
} 

- (CGRect)convertRect:(CGRect)sourceRect fromContentSize:(CGSize)sourceSize { 
    CGRect targetRect = sourceRect; 
    CGSize targetSize = self.bounds.size; 

    CGFloat ratioX = targetSize.width/sourceSize.width; 
    CGFloat ratioY = targetSize.height/sourceSize.height; 

    if (self.contentMode == UIViewContentModeScaleToFill) { 
     targetRect.origin.x *= ratioX; 
     targetRect.origin.y *= ratioY; 

     targetRect.size.width *= ratioX; 
     targetRect.size.height *= ratioY; 
    } 
    else if(self.contentMode == UIViewContentModeScaleAspectFit) { 
     CGFloat scale = MIN(ratioX, ratioY); 

     targetRect.origin.x *= scale; 
     targetRect.origin.y *= scale; 

     targetRect.origin.x += (self.frame.size.width - sourceSize.width * scale)/2.0f; 
     targetRect.origin.y += (self.frame.size.height - sourceSize.height * scale)/2.0f; 

     targetRect.size.width *= scale; 
     targetRect.size.height *= scale; 
    } 
    else if(self.contentMode == UIViewContentModeScaleAspectFill) { 
     CGFloat scale = MAX(ratioX, ratioY); 

     targetRect.origin.x *= scale; 
     targetRect.origin.y *= scale; 

     targetRect.origin.x += (self.frame.size.width - sourceSize.width * scale)/2.0f; 
     targetRect.origin.y += (self.frame.size.height - sourceSize.height * scale)/2.0f; 

     targetRect.size.width *= scale; 
     targetRect.size.height *= scale; 
    } 

    return targetRect; 
} 

및 최적화, 나는 그것을 github에 게시하고 여기에 링크를 게시 할 것입니다. 그 발췌 문장은 누군가에게 도움이 될지도 모릅니다.

+1

게시 한 적이 있습니까? –

+0

오늘 제가 한 다른 게시물을 참조하십시오. 나는 당신이 그것을 사용할 수 있기를 바랍니다! – nubbel

관련 문제