Call a C ++ class in a c one object

I wrote a C ++ class that returns uiimage (I made sure that it does not return null), and I'm trying to call it from object c obe (classes based on uiview) as shown below, the problem is that the image is not any suggestion for solving this issue is displayed, and also, if you put the code of the C ++ class in the object class c, an image will appear

image images = new images (); // C ++ file of class .mm

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:ImageFileName]; NSLog(fullPath); @try { UIView* holderView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height)]; viewer = [[UIImageView alloc] initWithFrame:[holderView frame]]; [viewer setImage:imaging->LoadImage(fullPath)]; viewer.contentMode = UIViewContentModeScaleAspectFit; //holderView.contentMode = UIViewContentModeScaleAspectFit ; [holderView addSubview:viewer]; [self.view addSubview:holderView]; } @catch (NSException * e) { NSLog(@"this was what happened: %a ",e); } 

This is the part of the boot image that creates the image from the buffer data.

try {...............

CGDataProviderRef provider = CGDataProviderCreateWithData (NULL, MyFilter-> GetOutput () → GetBufferPointer (), bufferLength, NULL); if (colorSpaceRef == NULL) {NSLog (@ "Error allocating color space"); CGDataProviderRelease (provider); }

  CGImageRef iref = CGImageCreate(Width,Height,bitsPerComponent,bitsPerPixel,bytesPerRow, colorSpaceRef,bitmapInfo,provider,NULL,YES,renderingIntent); CGContextRef context = NULL; if ((Comptype==itk::ImageIOBase::SHORT)||(Comptype==itk::ImageIOBase::USHORT)) { context = CGBitmapContextCreate(MyFilter->GetOutput()->GetBufferPointer(), Width, Height, bitsPerComponent, bytesPerRow, colorSpaceRef, bitmapInfo); } else { context = CGBitmapContextCreate(myimg->GetBufferPointer(), Width, Height, bitsPerComponent, bytesPerRow, colorSpaceRef, bitmapInfo); } if(context == NULL) { NSLog(@"Error context not created"); } //Create the UIImage to be displayed UIImage * uiimage = nil; if (context) { CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, Width, Height), iref); CGImageRef imageRef = CGBitmapContextCreateImage(context); //if([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) { //float scale = [[UIScreen mainScreen] scale]; //uiimage = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp]; //} else { uiimage = [UIImage imageWithCGImage:imageRef]; //} CGImageRelease(imageRef); CGContextRelease(context); } CGColorSpaceRelease(colorSpaceRef); CGImageRelease(iref); CGDataProviderRelease(provider); return uiimage; //return uiimage; } catch (itk::ExceptionObject & e) { throw(e); } 
0
source share
3 answers

The code looks fine - Objective-C, Objective-C ++, and C ++ code should work well together, so I assume that the problem could be within the images :: LoadImage (), even if it works separately - can we see that function?

0
source

Have you confirmed that LoadImage returns a UIImage object?

 UIImage *image = imaging->LoadImage(fullPath)]; NSLog(@"className of image is \"%@\"", [image className]); [viewer setImage:image]; 
0
source

Sometimes Obj-C objects created by class functions in Cocoa are auto-implemented. You may need to do:

 uiimage = [UIImage imageWithCGImage:imageRef]; [uiimage retain]; 

Since this is on iOS, I find it necessary since iOS does not have a garbage collection such as OS X Cocoa.

0
source

Source: https://habr.com/ru/post/1341177/


All Articles