2010-06-27 2 views
15

나는 이미지를 미러링 할 수있는 방법이 있는지 알아 내려고하고 있습니다. 예를 들어, 누군가의 얼굴 사진을 찍은 다음 반으로 자르고 각면이 거울처럼 보일 때 얼굴이 어떻게 보이는지 보여줍니다. CGAffineTransform 함수에서 이와 같은 트릭이없는 것 같습니다. 그래픽 전문가가 도와주세요!어떻게하면 UIImagePickerController에서 UIImage 그림을 미러링합니까

답변

34

여기서 기본 "트릭"은 X 축 또는 Y 축에 대한 배율 변환을 -1 배수로 사용하는 것입니다.

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

이 그럼 당신은 할당 된 이미지를 뒤집을 수있는 UIImageViewtransform 속성을 설정하거나 할 변환 서로를 연결하여 예를 들어, "수평 축에 대한 플립"을 만들려면이 사용할 수 변환 보다 정교한 효과. 기술 한 정확한 효과를 얻으려면 원본 이미지를 컨텍스트에 그리는 사용자 지정 드로잉 코드를 작성한 다음 반전 된 반을 위에 오버레이해야 할 수 있습니다. 이것은 Core Graphics에서 비교적 간단합니다.

+4

또는 CGAffineTransformMakeScale 즉 picker.cameraViewTransform = CGAffineTransformMakeScale 사용 (-1, 1) // SWIFT – khunshan

+1

우수 : 용액을 혼합하면 예를 UIImage + 미러링 카테고리로 사용할 수있는 다음 샘플 함수 리드! 'myImageView.transform = CGAffineTransformMakeScale (-1, 1); '을 사용했습니다. –

19

경우에만 않을까 긴 switch 문 귀찮게 왜 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]; 

그리고 당신은 열거를 살펴 경우는 모듈러 산술 할 것이라고 볼 수

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

그러나이 코드는 너무 영리하다. 대신 명시적인 switch 문을 사용하여 함수를 작성해야합니다. 예 :

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

실제로 응답이 좋으면 모든 경우가 아닙니다. 예 : 수평 뒤집기를하고 싶지만 이미지 방향이 맞다면 LeftMirrored를해야합니다. RightMirrored가 수직 뒤집기를하게됩니다. – AncAinu

+0

미러되지 않은 미러에서 미러 된 미러로 이동하면 자리에 앉아있는 사람의 관점에서 이미지가 수평으로 대칭됩니다. 이미지 자체의 관점에서 보면 열거 형의 주석에 따라 뒤집을 것입니다.면이 올바른 방향 (이미지의 맨 위부터 맨 위)으로 향한 경우 미러되지 않은 이미지에서부터 미러링하면 해당면을 수평으로 (즉, 수직 축을 따라) 뒤집을 수 있습니다. UIImageOrientation에 대한 문서 : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/#//apple_ref/c/tdef/UIImageOrientation. – sabalaba

+0

나는 내 대답을 편집하여 명시적인 switch 문을 사용하도록 제안했습니다. 길 아래에서 어떤 일이 일어날 지 누가 알겠습니까? 어쩌면 애플은 열거 형의 순서를 변경하여 모듈러 산술 버전이 부정확해질 수 있습니다. – sabalaba

5

위의 답변 중 아무 것도 이미지 전체를 뒤집지 않는 이미지의 절반을 반영하는 부분에 응답하십시오.

(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

관련 문제