在目标c中调用c ++类 [英] call c++ class within objective c one
问题描述
我写了c ++类返回uiimage(我确保它不返回null),我试图从目标c obe(基于uiview的类)调用如下代码,问题是没有图像显示任何建议要解决的,也是当把c ++类的代码放在目标c类时,图像出现
images imaging = new images(); // c ++ class .mm文件
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
是);
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(@这是发生了什么:%a,e);
}
这是loadimage的一部分, / p>
试试{
...............
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,MyFilter-> GetOutput() - > GetBufferPointer(),bufferLength,NULL);
if(colorSpaceRef == NULL)
{
NSLog(@分配颜色空间错误);
CGDataProviderRelease(provider);
}
CGImageRef iref = CGImageCreate(Width,Height,bitsPerComponent,bitsPerPixel,bytesPerRow,colorSpaceRef,bitmapInfo, NULL,YES,renderIntent);
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(@错误上下文未创建);
}
//创建要显示的UIImage
UIImage * uiimage = nil;
if(context)
{
CGContextDrawImage(context,CGRectMake(0.0f,0.0f,Width,Height),iref);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
// if([UIImage respondingToSelector:@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
}
在Cocoa中由类函数创建的Obj-C对象被自动释放。您可能需要:
uiimage = [UIImage imageWithCGImage:imageRef];
[uiimage retain];
因为这是在iOS上我相信这是必需的,因为iOS没有垃圾收集像OS X可可。
I wrote c++ class that return uiimage (I made sure that it doesn't return null) and I am trying to call it from objective c obe (uiview based classs) as below code, the problem is that no image be displayed any suggestion to solve that , also when put the code of c++ class at the objective c class the image appear
images imaging = new images(); // the c++ class .mm file
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 a part of the loadimage, which create 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);
}
Sometimes the Obj-C objects created by class functions in Cocoa are autoreleased. You may need to do:
uiimage = [UIImage imageWithCGImage:imageRef];
[uiimage retain];
Since this is on iOS I believe this is required, since iOS does not have garbage collection like OS X Cocoa.
这篇关于在目标c中调用c ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!