title: Images being rotated when converted from Matrix
body: <p>I am working on a app and making use of the <strong>opencv</strong> library. </p>
  
  <p>The problem I am having happens only to certain images (usually if made with the phone's camera) and I pinpointed as being just a conversion problem. When I convert a (problematic) Image to a cv::Mat object and then back it just rotates 90 degrees.</p>
  
  <p>Here is the call that causes the problem:</p>
  
  <pre><code>cv::Mat tmpMat = [sentImage CVMat];
  
  UIImage * tmpImage = [[UIImage alloc] initWithCVMat:tmpMat];
  
  [imageHolder setImage: tmpImage];
  </code></pre>
  
  <p>And here are the functions that do the conversion from image to matrix and vice-versa.</p>
  
  <pre><code>-(cv::Mat)CVMat
  {
  
  CGColorSpaceRef colorSpace = CGImageGetColorSpace(self.CGImage);
  CGFloat cols = self.size.width;
  CGFloat rows = self.size.height;
  
  cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels
  
  CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to backing data
                                                  cols,                      // Width of bitmap
                                                  rows,                     // Height of bitmap
                                                  8,                          // Bits per component
                                                  cvMat.step[0],              // Bytes per row
                                                  colorSpace,                 // Colorspace
                                                  kCGImageAlphaNoneSkipLast |
                                                  kCGBitmapByteOrderDefault); // Bitmap info flags
  
  CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), self.CGImage);
  CGContextRelease(contextRef);
  
  return cvMat;
  }
  
  
  - (id)initWithCVMat:(const cv::Mat&amp;)cvMat
  {
  NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize() * cvMat.total()];
  
  CGColorSpaceRef colorSpace;
  
  if (cvMat.elemSize() == 1)
  {
      colorSpace = CGColorSpaceCreateDeviceGray();
  }
  else
  {
      colorSpace = CGColorSpaceCreateDeviceRGB();
  }
  
  CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
  
  CGImageRef imageRef = CGImageCreate(cvMat.cols,                                     // Width
                                      cvMat.rows,                                     // Height
                                      8,                                              // Bits per component
                                      8 * cvMat.elemSize(),                           // Bits per pixel
                                      cvMat.step[0],                                  // Bytes per row
                                      colorSpace,                                     // Colorspace
                                      kCGImageAlphaNone | kCGBitmapByteOrderDefault,  // Bitmap info flags
                                      provider,                                       // CGDataProviderRef
                                      NULL,                                           // Decode
                                      false,                                          // Should interpolate
                                      kCGRenderingIntentDefault);                     // Intent
  
  self = [self initWithCGImage:imageRef];
  CGImageRelease(imageRef);
  CGDataProviderRelease(provider);
  CGColorSpaceRelease(colorSpace);
  
  return self;
  }
  </code></pre>
  
  <p>Now I am using a "Aspect Fill" property in my imageHolder (a <strong>UIImageView</strong>) and tried changing it without success. I also tried seeing if it was a problem of a matrix being transposed on the conversion and tried to change without success and it also would not be logical since it does not turn every picture.</p>
  
  <p>I do not understand why it works with some pictures but other not (all photos taken with the phone's camera don't work).</p>
  
  <p>If anyone can shed a light on the matter I would appreciate.</p>