Commit d647261d authored by themightyoarfish's avatar themightyoarfish

Fixed alpha preservation when converting 4-channel Mat to UIImage

parent 424c2bdd
...@@ -66,6 +66,10 @@ UIImage* MatToUIImage(const cv::Mat& image) { ...@@ -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) { ...@@ -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,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment