मेरे पास एक छवि के साथ 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);
}
यह 'getBoundingRectAfterRotation' फ़ंक्शन' क्या है? – bmdhacks
हाय @bmdhacks getBoundingRectAfterRotation एक विधि है जिसे मैंने बनाया है, इसलिए आप इसे कोको-टच कक्षाओं में से किसी एक में नहीं ढूंढ पाएंगे। यह तर्क के रूप में एक आयताकार और कोण लेता है और एक नया सीजीआरईटी देता है जो मूल कोण को शामिल करता है अगर उसे दिए गए कोण से घुमाया जाता है। लोकप्रिय मांग के कारण –
हाय @bmdhacks ने मेरे मूल प्रश्न के संपादन में विधि को जोड़ा है। –