2010-06-27 5 views
15

से एक UIImage चित्र दर्पण है। उदाहरण के लिए, किसी के चेहरे की तस्वीर लें और फिर इसे आधे में काट लें और दिखाएं कि उनका चेहरा प्रतिबिंबित प्रत्येक पक्ष के साथ कैसा दिखता है। CGAffineTransform फ़ंक्शंस में ऐसा कोई प्रतीत नहीं होता है। ग्राफिक्स विशेषज्ञों कृपया मदद करें !!!मैं कैसे मैं यह पता लगाने की अगर वहाँ किसी भी तरह से एक छवि को प्रतिबिंबित करें कोशिश कर रहा हूँ UIImagePickerController

उत्तर

34

बुनियादी "चाल" यहाँ एक स्केलिंग एक्स या -1 का एक पहलू के साथ Y अक्ष के बारे में बदलने का प्रयोग है। उदाहरण के लिए, आप इस इस्तेमाल कर सकते हैं एक "क्षैतिज अक्ष के बारे में फ्लिप" बनाने के लिए परिणत:

CGAffineTransform transform = CGAffineTransformScale(transform, -1, 1); 

तो फिर तुम सौंपा छवि फ्लिप करने के लिए एक UIImageView पर transform संपत्ति सेट कर सकते हैं, या किसी अन्य के साथ जोड़ करने के लिए बदलना अधिक परिष्कृत प्रभाव। आपके द्वारा वर्णित सटीक प्रभाव प्राप्त करने के लिए, आपको अपनी मूल छवि को संदर्भ में खींचने के लिए कुछ कस्टम ड्राइंग कोड लिखने की आवश्यकता हो सकती है, फिर इसके ऊपर फ़्लिप किए गए आधे को ओवरले करें। कोर ग्राफिक्स में यह अपेक्षाकृत सरल है।

+4

या CGAffineTransformMakeScale अर्थात picker.cameraViewTransform = CGAffineTransformMakeScale का उपयोग (-1, 1) // तेज – khunshan

+1

बहुत बढ़िया! मैं इस्तेमाल किया 'myImageView.transform = CGAffineTransformMakeScale (-1, 1);' –

19

आप केवल 4.0+

UIImageOrientation flippedOrientation = UIImageOrientationUpMirrored; 
switch (image.imageOrientation) { 
    case UIImageOrientationUp: break; 
    case UIImageOrientationDown: flippedOrientation = UIImageOrientationDownMirrored; break; 
    // ... 
} 
UIImage * flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:flippedOrientation]; 
12

आप सोच सकते हैं, क्यों अपमान लंबे स्विच बयान से परेशान समर्थन करना चाहते हैं तो?

? UIImage *flip = [UIImage imageWithCGImage:image.CGImage 
?          scale:image.scale 
?        orientation:(image.imageOrientation + 4) % 8]; 

और अगर आप enum पर एक नज़र डालें तो आप देख सकते हैं कि मॉड्यूलर अंकगणितीय करना होगा:

typedef NS_ENUM(NSInteger, UIImageOrientation) { 
    UIImageOrientationUp,   // default orientation 
    UIImageOrientationDown,   // 180 deg rotation 
    UIImageOrientationLeft,   // 90 deg CCW 
    UIImageOrientationRight,   // 90 deg CW 
    UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip 
    UIImageOrientationDownMirrored, // horizontal flip 
    UIImageOrientationLeftMirrored, // vertical flip 
    UIImageOrientationRightMirrored, // vertical flip 
}; 

लेकिन इस कोड बहुत चालाक है। आपको इसके बजाय एक स्पष्ट स्विच स्टेटमेंट के साथ एक फ़ंक्शन लिखना चाहिए। जैसे

UIImageOrientation mirroredImageOrientation(UIImageOrientation orientation) { 
    switch(orientation) { 
     case UIImageOrientationUp: return UIImageOrientationUpMirrored; 
     case UIImageOrientationDown: return UIImageOrientationDownMirrored; 
     case UIImageOrientationLeft: return UIImageOrientationLeftMirrored; 
     case UIImageOrientationRight: return UIImageOrientationRightMirrored; 
     case UIImageOrientationUpMirrored: return UIImageOrientationUp; 
     case UIImageOrientationDownMirrored: return UIImageOrientationDown; 
     case UIImageOrientationLeftMirrored: return UIImageOrientationLeft; 
     case UIImageOrientationRightMirrored: return UIImageOrientationRight; 
     default: return orientation; 
    } 
} 

और इस तरह समारोह का उपयोग करें:

UIImage *flip = [UIImage imageWithCGImage:image.CGImage 
            scale:image.scale 
           orientation:mirroredImageOrientation(image.imageOrientation)]; 

मैं संदिग्ध, बदबूदार कोड इंगित करने के लिए प्रश्न चिह्न जोड़ दिया है। The Practice of Programming

+1

वास्तव में अपने जवाब अच्छा लगता है, यह नहीं हर मामले में है। उदा। यदि आप क्षैतिज फ्लिप करना चाहते हैं लेकिन आपकी छवि अभिविन्यास सही है, तो आपको बाएं मिरर करना होगा, क्योंकि राइटमियरर्ड लंबवत फ्लिप देगा। – AncAinu

+0

से जा रहे हैं करने के लिए नजर आता क्षैतिज छवि दर्पण से आप अपने सीट पर नीचे बैठे के नजरिए से, गैर नजर आता। (आप सही है जो अपने आप छवि के नजरिए से यह enum में एनोटेशन के अनुसार फ्लिप जाएगा रहे हैं।) चेहरे उचित दिशा (छवि के शीर्ष करने के लिए सिर के ऊपर) में उन्मुख रहे हैं तो से सभी परिवर्तनों को गैर नजर आता प्रतिबिंब उस चेहरे को क्षैतिज रूप से फ़्लिप करेगा (यानी एक लंबवत धुरी के साथ)। UIImageOrientation के लिए प्रलेखन: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/#//apple_ref/c/tdef/UIImageOrientation। – sabalaba

+0

मैंने एक स्पष्ट स्विच स्टेटमेंट का उपयोग करके सुझाव देने के लिए अपना उत्तर संपादित किया। कौन जानता है कि सड़क के नीचे क्या होगा। हो सकता है कि ऐप्पल enum के क्रम को बदल देगा, जिस स्थिति में मॉड्यूलर अंकगणितीय संस्करण गलत हो जाता है। – sabalaba

5

उपरोक्त में से कोई भी जवाब, प्रश्न के उस हिस्से का जवाब दें जो छवि के आधे हिस्से को मिरर कर रहा है, पूरी छवि को फ़्लिप नहीं कर रहा है। समाधान मिश्रण निम्न नमूना समारोह आप इस तरह के UIImage + मिररिंग के रूप में एक वर्ग के रूप में उपयोग कर सकते हैं की ओर जाता है:

(UIImage *) horizontalMirror { 
    UIImageOrientation flippedOrientation = UIImageOrientationUpMirrored; 
    switch (self.imageOrientation) { 
     case UIImageOrientationUp: break; 
     case UIImageOrientationDown: flippedOrientation = UIImageOrientationDownMirrored; break; 
    } 
    UIImage * flippedImage = [UIImage imageWithCGImage:self.CGImage scale:1.0 orientation:flippedOrientation]; 

    CGImageRef inImage = self.CGImage; 
    CGContextRef ctx = CGBitmapContextCreate(NULL, 
              CGImageGetWidth(inImage), 
              CGImageGetHeight(inImage), 
              CGImageGetBitsPerComponent(inImage), 
              CGImageGetBytesPerRow(inImage), 
              CGImageGetColorSpace(inImage), 
              CGImageGetBitmapInfo(inImage) 
              ); 
    CGRect cropRect = CGRectMake(flippedImage.size.width/2, 0, flippedImage.size.width/2, flippedImage.size.height); 
    CGImageRef TheOtherHalf = CGImageCreateWithImageInRect(flippedImage.CGImage, cropRect); 
    CGContextDrawImage(ctx, CGRectMake(0, 0, CGImageGetWidth(inImage), CGImageGetHeight(inImage)), inImage); 

    CGAffineTransform transform = CGAffineTransformMakeTranslation(flippedImage.size.width, 0.0); 
    transform = CGAffineTransformScale(transform, -1.0, 1.0); 
    CGContextConcatCTM(ctx, transform); 

    CGContextDrawImage(ctx, cropRect, TheOtherHalf); 

    CGImageRef imageRef = CGBitmapContextCreateImage(ctx); 
    CGContextRelease(ctx); 
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 

    return finalImage; 
} 
+0

धन्यवाद यह काम किया – ChanWarde

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

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