title: opencv ios transparent image loaded without transparency
body: <p>I'm, developing an ios application that uses some opencv functionality (remove background, edge detections etc.)</p>
  
  <p>Amongst other things, I load transparent images (pngs). From some reason when I load the images opencv decides to ignore some transparent areas while respecting others. I can see this by converting my semi transparent UIImage to an IplImage and then back to UIImage (without doing any operation on the iplimage) and saving both images to the local file.</p>
  
  <p>This is the code I'm using for converting the UIImage to an IplImage and backwards:</p>
  
  <pre><code>- (IplImage *)CreateIplImageFromUIImageWithTransparency:(UIImage *)image {
      // Getting CGImage from UIImage
      CGImageRef imageRef = image.CGImage;
  
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
      // Creating temporal IplImage for drawing
      IplImage *iplimage = cvCreateImage(
                                         cvSize(image.size.width,image.size.height), IPL_DEPTH_8U, 4
                                         );
      // Creating CGContext for temporal IplImage
      CGContextRef contextRef = CGBitmapContextCreate(
                                                      iplimage-&gt;imageData, iplimage-&gt;width, iplimage-&gt;height,
                                                      iplimage-&gt;depth, iplimage-&gt;widthStep,
                                                      colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault
                                                      );
      // Drawing CGImage to CGContext
      CGContextDrawImage(
                         contextRef,
                         CGRectMake(0, 0, image.size.width, image.size.height),
                         imageRef
                         );
      CGContextRelease(contextRef);
      CGColorSpaceRelease(colorSpace);
  
      // Creating result IplImage
      IplImage *ret = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 4);
      cvCvtColor(iplimage, ret, CV_RGBA2BGRA);
      cvReleaseImage(&amp;iplimage);
  
      return ret;
  }
  
  
  - (UIImage *)UIImageFromIplImage:(IplImage *)image {
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
      NSData *data = [NSData dataWithBytes:image-&gt;imageData length:image-&gt;imageSize];
      CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
      CGImageRef imageRef = CGImageCreate(image-&gt;width, image-&gt;height,
                                          image-&gt;depth, image-&gt;depth * image-&gt;nChannels, image-&gt;widthStep,
                                          colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaLast ,
                                          provider, NULL, false, kCGRenderingIntentDefault);
      UIImage *ret = [UIImage imageWithCGImage:imageRef];
      CGImageRelease(imageRef);
      CGDataProviderRelease(provider);
      CGColorSpaceRelease(colorSpace);
      return ret;
  }
  </code></pre>
  
  <p>What could be causing this behavior?</p>
  
  <p>Thank you</p>