Fixed alpha preservation when converting 4-channel Mat to UIImage

This commit is contained in:
themightyoarfish
2015-06-11 14:59:57 +02:00
parent 424c2bddb3
commit d647261de2

View File

@@ -66,6 +66,10 @@ UIImage* MatToUIImage(const cv::Mat& image) {
CGDataProviderRef provider = CGDataProviderRef provider =
CGDataProviderCreateWithCFData((__bridge CFDataRef)data); CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
// Preserve alpha transparency, if exists
bool alpha = cvMat.channels() == 4;
CGBitmapInfo bitMapInfo = (alpha ? kCGImageAlphaLast : kCGImageAlphaNone) | kCGBitmapByteOrderDefault;
// Creating CGImage from cv::Mat // Creating CGImage from cv::Mat
CGImageRef imageRef = CGImageCreate(image.cols, CGImageRef imageRef = CGImageCreate(image.cols,
image.rows, image.rows,
@@ -73,8 +77,7 @@ UIImage* MatToUIImage(const cv::Mat& image) {
8 * image.elemSize(), 8 * image.elemSize(),
image.step.p[0], image.step.p[0],
colorSpace, colorSpace,
kCGImageAlphaNone| bitmapInfo,
kCGBitmapByteOrderDefault,
provider, provider,
NULL, NULL,
false, false,