2010-05-18 7 views
15

मेरे पास एक छवि के साथ UIImageView है। मैंने यूआईएममेज व्यू की ट्रांसफॉर्म प्रॉपर्टी को CGAffineTransformMakeRotation (कोण) में सेट करके प्रदर्शित करने से पहले छवि को घुमाया है जहां कोण रेडियंस में कोण है।एक घुमावदार UIImageView से UIImage बनाना

मैं एक और यूआईएममेज बनाने में सक्षम होना चाहता हूं जो घुमावदार संस्करण से मेल खाता है जो मैं अपने विचार में देख सकता हूं।

- (UIImage *) rotatedImageFromImageView: (UIImageView *) imageView 
{ 
    UIImage *rotatedImage; 

    // Get image width, height of the bounding rectangle 
    CGRect boundingRect = [self getBoundingRectAfterRotation: imageView.bounds byAngle:angle]; 

    // Create a graphics context the size of the bounding rectangle 
    UIGraphicsBeginImageContext(boundingRect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Rotate and translate the context 
    CGAffineTransform ourTransform = CGAffineTransformIdentity; 
    ourTransform = CGAffineTransformConcat(ourTransform, CGAffineTransformMakeRotation(angle)); 

    CGContextConcatCTM(context, ourTransform); 

    // Draw the image into the context 
    CGContextDrawImage(context, CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage); 

    // Get an image from the context 
    rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)]; 

    // Clean up 
    UIGraphicsEndImageContext(); 
    return rotatedImage; 
} 

हालांकि छवि अपने केंद्र के बारे में घुमाया नहीं है:

मैं लगभग वहाँ हूँ, छवि संदर्भ घूर्णन द्वारा मैं एक घुमाया छवि मिलता है। मैंने अपने घूर्णन के साथ समेकित सभी प्रकार के परिवर्तनों को केंद्र के चारों ओर घूमने के लिए प्रयास किया है, लेकिन इसका कोई फायदा नहीं हुआ है। क्या मुझे एक चाल लापता है? क्या यह संभव है क्योंकि मैं संदर्भ को घुमा रहा हूं, छवि नहीं?

अब यह काम करने के लिए बेताब हो रहा है, इसलिए किसी भी मदद की सराहना की जाएगी।

डेव

संपादित: मैं अपने boundingRect कोड के लिए कई बार कहा गया है, इसलिए यहाँ यह है:

- (CGRect) getBoundingRectAfterRotation: (CGRect) rectangle byAngle: (CGFloat) angleOfRotation { 
    // Calculate the width and height of the bounding rectangle using basic trig 
    CGFloat newWidth = rectangle.size.width * fabs(cosf(angleOfRotation)) + rectangle.size.height * fabs(sinf(angleOfRotation)); 
    CGFloat newHeight = rectangle.size.height * fabs(cosf(angleOfRotation)) + rectangle.size.width * fabs(sinf(angleOfRotation)); 

    // Calculate the position of the origin 
    CGFloat newX = rectangle.origin.x + ((rectangle.size.width - newWidth)/2); 
    CGFloat newY = rectangle.origin.y + ((rectangle.size.height - newHeight)/2); 

    // Return the rectangle 
    return CGRectMake(newX, newY, newWidth, newHeight); 
} 
+0

यह 'getBoundingRectAfterRotation' फ़ंक्शन' क्या है? – bmdhacks

+0

हाय @bmdhacks getBoundingRectAfterRotation एक विधि है जिसे मैंने बनाया है, इसलिए आप इसे कोको-टच कक्षाओं में से किसी एक में नहीं ढूंढ पाएंगे। यह तर्क के रूप में एक आयताकार और कोण लेता है और एक नया सीजीआरईटी देता है जो मूल कोण को शामिल करता है अगर उसे दिए गए कोण से घुमाया जाता है। लोकप्रिय मांग के कारण –

+0

हाय @bmdhacks ने मेरे मूल प्रश्न के संपादन में विधि को जोड़ा है। –

उत्तर

22

ठीक है - पिछले पर मैं इसे किया है लगते हैं। शुद्धता पर कोई भी टिप्पणी उपयोगी होगी ... इसे अनुवाद करने के लिए एक अनुवाद, घूर्णन, एक पैमाने और ड्राइंग रेक्ट स्थिति से ऑफसेट की आवश्यकता होती है। कोड यहां है:

CGAffineTransform transform = CGAffineTransformIdentity; 
transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2); 
transform = CGAffineTransformRotate(transform, angle); 
transform = CGAffineTransformScale(transform, 1.0, -1.0); 

CGContextConcatCTM(context, transform); 

// Draw the image into the context 
CGContextDrawImage(context, CGRectMake(-imageView.image.size.width/2, -imageView.image.size.height/2, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage); 

// Get an image from the context 
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)]; 
+1

आपको 'ट्रांसफॉर्म = CGAffineTransformScale (ट्रांसफॉर्म, 1.0, -1.0) क्यों चाहिए; ' ? – Hlung

+3

कोरग्राफिक्स एक समन्वय प्रणाली का उपयोग करता है जहां मूल नीचे बाईं ओर है। जब आप स्क्रीन को नीचे ले जाते हैं तो यूआईकिट ऑब्जेक्ट्स वाई-अक्ष बढ़ने के साथ ऊपरी बाईं ओर एक उत्पत्ति का उपयोग करते हैं। यह परिवर्तन यह सुनिश्चित करने के लिए वाई-अक्ष को फ़्लिप करता है कि छवि सही तरीके से खींची जाती है। –

+1

धन्यवाद! मैं अनुवाद, रोटेशन और स्केलिंग के संदर्भ में छवि को चित्रित करने पर संघर्ष कर रहा था। मैंने UIImage की 'drawInRect:' की कोशिश की और संदर्भ का अनुवाद किया लेकिन यह हमेशा शीर्ष बाएं (0,0) के आसपास घूमता है। आपका समाधान केवल एक ही काम करता है! :) – Hlung