2011-08-27 8 views
15

मान लें कि मेरे पास एक छवि है (उदा। 1024 x 768 पीएक्स) जो UIImageView (उदा। 300 x 300 पीएक्स) में प्रदर्शित होती है। अब, मैं छवि से एक बिंदु को कन्वर्ट करना चाहता हूं, उदा। किसी व्यक्ति की नाक की स्थिति (x: 500, y: 600), UIImageView पर इसी बिंदु पर इसकी सामग्री के साथ मोड को ध्यान में रखा गया।छवि से UIImageView बिंदुओं में बिंदुओं को कनवर्ट करें, सामग्री मोड-जागरूक

यदि सामग्री मोड UIViewContentModeScaleToFill पर तय किया गया है, तो रूपांतरण आसान होगा। लेकिन अगर यह UIViewContentModeScaleAspectFit है, तो चीजें अधिक जटिल हो रही हैं।

क्या यह हासिल करने का एक शानदार तरीका है? मैं वास्तव में गणना नहीं करना चाहता कि प्रत्येक सामग्री के लिए मोड (10 से अधिक, मुझे लगता है)।

धन्यवाद!

उत्तर

29

आज, मैं इस समस्या का एक साथ मेरी समाधान डाल दिया और GitHub पर प्रकाशित करने के लिए कुछ खाली समय मिला है: UIImageView-GeometryConversion

यह UIImageView पर एक वर्ग है कि देखने के लिए छवि से अंक और rects परिवर्तित करने के लिए तरीके प्रदान है सभी 13 अलग-अलग सामग्री मोडों का समन्वय और सम्मान करता है।

आशा है कि आप इसे पसंद करेंगे!

+0

बहुत अच्छा - यह भी अच्छा होगा यदि आपने अन्य तरीकों को भी प्रदान किया था (छवि दृश्य से समन्वय से छवि समन्वय)। – sarfata

+0

आप सही हैं, मैं इसे जल्द ही जोड़ दूंगा। – nubbel

+1

परेशान न करें, मैंने इसे कुछ तरीकों से किया है, मैं इसे अभी खत्म कर रहा हूं और आज आपको जिथब पर एक पुल अनुरोध भेजूंगा। – sarfata

2

नहीं, ऐसा करने के लिए कोई अंतर्निहित, सार्वजनिक या सुरुचिपूर्ण तरीका नहीं है।

आपको अपनी आवश्यक सामग्री मोड के कार्यों को इंजीनियर करने की आवश्यकता है।

+0

धन्यवाद, मैं इसे UIViewContentModeScaleToFill, UIViewContentModeAspectFit और UIViewContentModeAspectFill के लिए करूँगा। यह पर्याप्त होना चाहिए। – nubbel

+2

@nubbel - जब किया जाता है तो कृपया इसे गिटूब या किसी अन्य तरीके से सार्वजनिक बनाएं, ताकि अगले व्यक्ति को उसी हुप्स से कूदना पड़े। – PeyloW

-1

आप क्यों नहीं बस निर्देशांक rescale नहीं कर सकते:

एक्स (UIImageView) = (एक्स (छवि)/1024) * 300

और

वाई (UIImageView) = (वाई (छवि)/768) * 300

+0

यह UIViewContentModeScaleToFill के लिए अच्छा है, लेकिन अन्य सामग्री मोड के लिए? – nubbel

+0

UIContentModeScaleToFit के लिए काम नहीं करता है क्योंकि मार्जिन दिखाई देगा और छवि uiimageview – Vassily

10

मेरी quick'n'dirty समाधान है कि:

- (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; 
} 

जब यह पुनर्संशोधित है और अनुकूलित, मैं इसे गीथब पर प्रकाशित करूंगा और लिंक को यहां पोस्ट कर दूंगा। शायद यह भी स्निपेट किसी के लिए सहायक हो सकता है।

+1

में केंद्रित होगी क्या आपने कभी इसे प्रकाशित किया था? –

+0

मैंने आज किया, मेरी दूसरी पोस्ट देखें। मुझे उम्मीद है कि आप इसका इस्तेमाल कर सकते हैं! – nubbel

संबंधित मुद्दे