Fix MatToUIImage method crash when the step is not equal to cols * elemSize()
authorChaoshuai Lu <lcs@Chaoshuais-Pro.dhcp.thefacebook.com>
Thu, 23 Mar 2017 05:04:27 +0000 (22:04 -0700)
committerChaoshuai Lu <lcs@Chaoshuais-Pro.dhcp.thefacebook.com>
Thu, 23 Mar 2017 05:04:27 +0000 (22:04 -0700)
modules/imgcodecs/src/ios_conversions.mm

index eed867a..2db873a 100644 (file)
@@ -53,7 +53,7 @@ void UIImageToMat(const UIImage* image, cv::Mat& m, bool alphaExist);
 UIImage* MatToUIImage(const cv::Mat& image) {
 
     NSData *data = [NSData dataWithBytes:image.data
-                                  length:image.elemSize()*image.total()];
+                                  length:image.step.p[0] * image.rows];
 
     CGColorSpaceRef colorSpace;
 
@@ -73,7 +73,7 @@ UIImage* MatToUIImage(const cv::Mat& image) {
     // Creating CGImage from cv::Mat
     CGImageRef imageRef = CGImageCreate(image.cols,
                                         image.rows,
-                                        8,
+                                        8 * image.elemSize1(),
                                         8 * image.elemSize(),
                                         image.step.p[0],
                                         colorSpace,