2011-08-25 13 views
7

में छवि घुमाएं क्या UIImageView में केवल छवि को घुमाने के लिए संभव है? मैं इसके बारे में जानकारी ढूंढ रहा हूं, लेकिन मुझे केवल जानकारी मिली है कि UIImageVeiw को कैसे घुमाया जाए।UIImageView

उत्तर

15

आप छवि को निम्न कोड से घुमा सकते हैं।

UIImage* img = [UIImage imageWithCGImage: imageRef]; 
myImageView.image = img; 
यहाँ

: ध्यान दें, यह एक CGImageRef जो आप के माध्यम से

CGImageRef imageRef = [self CGImageRotatedByAngle:[image CGImage] angle:30]; 

एक बार जब आप घुमाया छवि मिलता है, आप इस तरह अपने नए घुमाया छवि के लिए imageView की छवि सेट कर सकते हैं एक UIImage से प्राप्त कर सकते हैं का उपयोग करता है एक विधि है कि imageRef बारी बारी से होगा:

- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle 
{ 

    CGFloat angleInRadians = angle * (M_PI/180); 
    CGFloat width = CGImageGetWidth(imgRef); 
    CGFloat height = CGImageGetHeight(imgRef); 

    CGRect imgRect = CGRectMake(0, 0, width, height); 
    CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians); 
    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform); 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    CGContextRef bmContext = CGBitmapContextCreate(NULL, 
                rotatedRect.size.width, 
                rotatedRect.size.height, 
                8, 
                0, 
                colorSpace, 
                kCGImageAlphaPremultipliedFirst); 
    CGContextSetAllowsAntialiasing(bmContext, YES); 
    CGContextSetShouldAntialias(bmContext, YES); 
    CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh); 
    CGColorSpaceRelease(colorSpace); 
    CGContextTranslateCTM(bmContext, 
          +(rotatedRect.size.width/2), 
          +(rotatedRect.size.height/2)); 
    CGContextRotateCTM(bmContext, angleInRadians); 
    CGContextTranslateCTM(bmContext, 
          -(rotatedRect.size.width/2), 
          -(rotatedRect.size.height/2)); 
    CGContextDrawImage(bmContext, CGRectMake(0, 0, 
              rotatedRect.size.width, 
              rotatedRect.size.height), 
         imgRef); 



    CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext); 
    CFRelease(bmContext); 
    [(id)rotatedImage autorelease]; 

    return rotatedImage; 
} 
+0

धन्यवाद आप। यह वही है जो मैं चाहता हूं। – murzynpl

+1

किसी ने अप्रत्याशित परिणाम का सामना किया है जैसा कि मैंने हाल ही में किया था - अंतिम छवि अपने मूल आकार और फसल वाले कोनों को खो रही थी। इसलिए मैंने सही परिणाम प्राप्त करने के लिए स्लिमिटी को फ़ंक्शन को संशोधित किया है। अगर कोई मेरा समाधान सोच रहा है तो यहां है: http://stackoverflow.com/questions/19219855/uiimageview-get-transformed-uiimage/19221081#19221081 – heximal

+0

रोटेशन के बाद छवि का आकार पिछले टिप्पणीकर्ता के रूप में बदल जाएगा। उपरोक्त कोड में एक मामूली बग है। CGContextTranslateCTM और CGContextDrawImage को अंतिम कॉल roundedRect.size.width और rounded.size.height की बजाय मूल चौड़ाई और ऊंचाई का उपयोग करना चाहिए। एक बार ऐसा करने के बाद, ऊपर दिया गया यह कोड पूरी तरह से काम करता है। कारण - घूर्णन वाले रेक्ट के साथ एक सीटीटीटी बनाया गया है, मूल केंद्र में स्थानांतरित हो गया है, फिर इसे मूल छवि अभिविन्यास से मेल खाने के लिए उलटा घुमाया जाता है। इस बिंदु पर आपको इसे मूल छवि के समन्वय प्रणाली में वापस ले जाने की आवश्यकता है। – matangs

6

ImageView की परत को घुमाने पर एक नज़र डालें। इसे सुलभ बनाने के लिए आपको क्वार्ट्जकोर लाइब्रेरी आयात करने की आवश्यकता होगी।

myImageView.layer.transform = CGAffineTransformMakeRotation (1.5); 
+1

का इस्तेमाल किया था 'setAffineTransform' – fabian789

5

इस प्रयास करें, यह मेरे लिए काम करता है:

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
[UIView setAnimationRepeatCount:10]; 
myImgView.transform = CGAffineTransformMakeRotation(M_PI); 
[UIView commitAnimations]; 

शुभकामनाएं!

1

निम्नलिखित कोड स्विफ्ट के साथ ठीक काम करता है:

let img = UIImage(CGImage: self.imageView.image!.CGImage, scale: 1.0, orientation: UIImageOrientation.Right) 
self.imageView.image = img 
self.imageView.setNeedsLayout() 
0

आप बारी बारी से करने की जरूरत है/स्थानीयकरण प्रयोजनों के लिए एक छवि फ्लिप आप निम्नलिखित का उपयोग कर सकते हैं: imageView.image = imageView.image?.imageFlippedForRightToLeftLayoutDirection()