@kyungtaek

  • About me
  • Photoworks
  • RSS
  • Archive

iOS 카메라 스트림에서 얻어낸 이미지에 Orientation code를 수동으로 넣기.

iOS 카메라 스트림에서 얻어낸 이미지의 Orientation은 원하는 것과 다를 경우가 많다. UIImage의 size를 찾아보면 세로가 긴 사진으로 나오는데 비해, CGImageGetWidth(uiimage.CGImage)를 수행해보면 가로가 긴 사진으로 데이터가 저장되어 있는 경우를 볼 수 있다. (즉, 카메라에서 캡쳐된 데이터는 특정한 방향으로만 저장되어 있는 상태이며, 미리 정의된 방향성 Flag-uiimage.imageOrientaion-만 지정해두고, 실제로 그릴때 이 Flag대로 방향성을 가지고 그리는 듯 하다.) 떄때로 CGimage를 저장할 때 ImageOrientation은 문제가 되기도 하는데, 이를 보정하기 위해 TIFF Exif 정보를 수동으로 넣어주는 방법이 있다. (사실 지금 진행하는 프로젝트에서는 이렇게 구현해놓고서는 이건 너무 무식하다 싶어서 삭제해버렸는데, 아이러니하게도 애플 개발자 사이트의 예제소스도 이와 같은 방법을 쓰고 있었다.) 또한 주의할 점은 Front camera와 Rear camera의 방향이 다르다는 점도.

UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation]; int exifOrientation;

/* kCGImagePropertyOrientation values
    The intended display orientation of the image. If present, this key is a CFNumber value with the same value as defined
    by the TIFF and EXIF specifications -- see enumeration of integer constants. 
    The value specified where the origin (0,0) of the image is located. If not present, a value of 1 is assumed.

    used when calling featuresInImage: options: The value for this key is an integer NSNumber from 1..8 as found in kCGImagePropertyOrientation.
    If present, the detection will be done based on that orientation but the coordinates in the returned features will still be based on those of the image. */

enum {
    PHOTOS_EXIF_0ROW_TOP_0COL_LEFT          = 1, //   1  =  0th row is at the top, and 0th column is on the left (THE DEFAULT).
    PHOTOS_EXIF_0ROW_TOP_0COL_RIGHT         = 2, //   2  =  0th row is at the top, and 0th column is on the right.  
    PHOTOS_EXIF_0ROW_BOTTOM_0COL_RIGHT      = 3, //   3  =  0th row is at the bottom, and 0th column is on the right.  
    PHOTOS_EXIF_0ROW_BOTTOM_0COL_LEFT       = 4, //   4  =  0th row is at the bottom, and 0th column is on the left.  
    PHOTOS_EXIF_0ROW_LEFT_0COL_TOP          = 5, //   5  =  0th row is on the left, and 0th column is the top.  
    PHOTOS_EXIF_0ROW_RIGHT_0COL_TOP         = 6, //   6  =  0th row is on the right, and 0th column is the top.  
    PHOTOS_EXIF_0ROW_RIGHT_0COL_BOTTOM      = 7, //   7  =  0th row is on the right, and 0th column is the bottom.  
    PHOTOS_EXIF_0ROW_LEFT_0COL_BOTTOM       = 8  //   8  =  0th row is on the left, and 0th column is the bottom.  
};

switch (curDeviceOrientation) {
    case UIDeviceOrientationPortraitUpsideDown:  // Device oriented vertically, home button on the top
        exifOrientation = PHOTOS_EXIF_0ROW_LEFT_0COL_BOTTOM;
        break;
    case UIDeviceOrientationLandscapeLeft:       // Device oriented horizontally, home button on the right
        if (isUsingFrontFacingCamera)
            exifOrientation = PHOTOS_EXIF_0ROW_BOTTOM_0COL_RIGHT;
        else
            exifOrientation = PHOTOS_EXIF_0ROW_TOP_0COL_LEFT;
        break;
    case UIDeviceOrientationLandscapeRight:      // Device oriented horizontally, home button on the left
        if (isUsingFrontFacingCamera)
            exifOrientation = PHOTOS_EXIF_0ROW_TOP_0COL_LEFT;
        else
            exifOrientation = PHOTOS_EXIF_0ROW_BOTTOM_0COL_RIGHT;
        break;
    case UIDeviceOrientationPortrait:            // Device oriented vertically, home button on the bottom
    default:
        exifOrientation = PHOTOS_EXIF_0ROW_RIGHT_0COL_TOP;
        break;
}

///< … CGDestinationImage를 이용하거나 CIImage에 해당 정보를 기록하거나…

저장하는 방법이야 쉽지만, Orientation에 따른 분기를 제대로 만들어놓지 않으면 매우 헷깔리는 경우 (왜 애플은 무식하게 이렇게 만들어놨나…)

Dec 4 2011
1 note
  • #tech
  • #ios
  1. loganabbott liked this
  2. kyungtaek posted this
Copyright © 2009–2012 @kyungtaek ‒ HD Exhibit Theme by Dustin Hoffman