在目标c中调用c ++类

我写的c ++类返回uiimage(我确信它不会返回null),我试图从目标c(基于uiview的类)如下代码调用它,问题是没有图像显示任何build议解决这个问题的时候,也把c ++类的代码放在客观的c类图像上出现

图像成像=新图像(); // c ++类.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); } 

这是loadimage的一部分,它从缓冲区数据创build图像

试试{……………

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,MyFilter-> GetOutput() – > GetBufferPointer(),bufferLength,NULL); 如果(colorSpaceRef == NULL){NSLog(@“Error allocating color space”); CGDataProviderRelease(提供商); }

  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); } 

代码看起来没问题 – Objective C,Objective C ++和C ++代码应该一起工作,所以我猜这个问题可能在images :: LoadImage()中,即使它单独工作 – 我们可以看到这个函数吗?

你确认LoadImage是否返回UIImage对象?

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

有时由Cocoa中的类函数创build的Obj-C对象是自动释放的。 您可能需要执行以下操作:

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

由于这是在iOS上我相信这是必需的,因为iOS没有像OS Xcocoa的垃圾收集。