Memory leaks IOS在图像内存泄漏上绘制图像
我试图用下面的方法在UIImageView中绘制文本。 问题是,该方法是从循环内部调用的,循环迭代32次(在本例中)。我在第14次电话“文本图像就绪”后就死了 不幸的是,当我使用仪器查找漏洞时,应用程序在我得到任何东西之前就死掉了。 我不太确定我应该发布什么,或者是否有更好的方法来做到这一点 谢谢Memory leaks IOS在图像内存泄漏上绘制图像,memory-leaks,uiimage,core-graphics,retina-display,drawimage,Memory Leaks,Uiimage,Core Graphics,Retina Display,Drawimage,我试图用下面的方法在UIImageView中绘制文本。 问题是,该方法是从循环内部调用的,循环迭代32次(在本例中)。我在第14次电话“文本图像就绪”后就死了 不幸的是,当我使用仪器查找漏洞时,应用程序在我得到任何东西之前就死掉了。 我不太确定我应该发布什么,或者是否有更好的方法来做到这一点 谢谢 -(void) writeNameToImage:(int)soundIndex setNumber:(int)setNumber { NSString* strTextToWrite;
-(void) writeNameToImage:(int)soundIndex setNumber:(int)setNumber
{
NSString* strTextToWrite;
if(setNumber==1)
strTextToWrite= ((MyObject*)[arrObjects1 objectAtIndex:soundIndex-1]).soundName;
else
strTextToWrite= ((MyObject*)[arrObjects2 objectAtIndex:soundIndex-1]).soundName;
NSLog(@"string to draw: %@",strTextToWrite);
//Retrieve image from UIImageView
UIImage* layerToUpdate= imgViewRecorded.image;
//***** Drawing the text image.
UIFont *font = [UIFont boldSystemFontOfSize:15.0];
CGSize size = [strTextToWrite sizeWithFont:font];
UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
// draw in context, you can use also drawInRect:withFont:
[[UIColor whiteColor] set];
[strTextToWrite drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];
// transfer image
UIImage* imgToAdd = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"text image ready");
//**** Finished drawing text image
//**** Updating layer image with generated image
UIGraphicsBeginImageContextWithOptions(layerToUpdate.size, FALSE, 0.0);
NSLog(@"layer created in context");
[layerToUpdate drawInRect:CGRectMake( 0, 0, layerToUpdate.size.width, layerToUpdate.size.height)];
//translate to the center
CGContextTranslateCTM( UIGraphicsGetCurrentContext(), 0.5f * layerToUpdate.size.width, 0.5f * layerToUpdate.size.height ) ;
//rotate with the center of the image as the rotation point
CGContextRotateCTM(UIGraphicsGetCurrentContext(), CC_DEGREES_TO_RADIANS( degrees ) );
//translate back to original position
CGContextTranslateCTM( UIGraphicsGetCurrentContext(), -0.5f *layerToUpdate.size.width, -0.5f *layerToUpdate.size.height ) ;
if(setNumber==1)
[imgToAdd drawInRect:CGRectMake( (303-(imgToAdd.size.width/2)), 80, imgToAdd.size.width, imgToAdd.size.height)];
else
[imgToAdd drawInRect:CGRectMake( (303-(imgToAdd.size.width/2)), 23, imgToAdd.size.width, imgToAdd.size.height)];
NSLog(@"text drawn to layer");
CGContextRotateCTM(UIGraphicsGetCurrentContext(), CC_DEGREES_TO_RADIANS( degrees ) );
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[layerToUpdate release];
NSLog(@"assigning new created image");
imgViewRecorded.image = newImage;
}
我注意到,如果我在方法末尾添加[newImage release]
,它将完全运行,但显然会导致EXC\u BAD\u访问
更新
我尝试过另一种解决方案,但在这种情况下,文本逐渐变得模糊。这意味着我画的第一个文本是最模糊的,最后一个看起来非常清晰。我认为这可能是由于视网膜鳞片,但我不知道如何修复
-(void) writeSoundNameToImage:(int)soundIndex setNumber:(int)setNumber
{
NSString* strTextToWrite;
if(setNumber==1)
strTextToWrite= ((MyObject*)[arrObjects1 objectAtIndex:soundIndex-1]).soundName;
else
strTextToWrite= ((MyObject*)[arrObjects2 objectAtIndex:soundIndex-1]).soundName;
NSLog(@"string to draw: %@",strTextToWrite);
//***** Drawing the text image.
UIFont *font = [UIFont boldSystemFontOfSize:15.0];
CGSize size = [strTextToWrite sizeWithFont:font];
UIImage* layerToUpdate= imgViewRecorded.image;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat scale = [[UIScreen mainScreen] scale];
CGContextRef context = CGBitmapContextCreate( NULL,layerToUpdate.size.width,layerToUpdate.size.height, 8, 4 * layerToUpdate.size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, layerToUpdate.size.width, layerToUpdate.size.height), layerToUpdate.CGImage);
//translate to the center
CGContextTranslateCTM( context, 0.5f * layerToUpdate.size.width, 0.5f * layerToUpdate.size.height ) ;
//rotate with the center of the image as the rotation point
CGContextRotateCTM(context, -CC_DEGREES_TO_RADIANS( ((soundIndex-1)*self.myProfile.degreesPerSound)+self.myProfile.degreesPerSound/2 ) );
//translate back to original position
CGContextTranslateCTM( context, -0.5f*layerToUpdate.size.width, -0.5f*layerToUpdate.size.height ) ;
char* text = (char *)[strTextToWrite cStringUsingEncoding:NSASCIIStringEncoding];
CGContextSelectFont(context, "Helvetica", 15, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
CGContextSetRGBStrokeColor(context, 255, 255, 255, 1);
if(setNumber==1)
CGContextShowTextAtPoint(context, (303-(size.width/2)), 600-90, text, strlen(text));
else
CGContextShowTextAtPoint(context, (303-(size.width/2)), 600-33, text, strlen(text));
CGContextRotateCTM(context, -CC_DEGREES_TO_RADIANS( ((soundIndex-1)*self.myProfile.degreesPerSound)+self.myProfile.degreesPerSound/2 ) );
//CGContextScaleCTM(context, layerToUpdate.size.width/scale, layerToUpdate.size.height/scale);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
UIImage* newImage=[UIImage imageWithCGImage:imageMasked scale:scale orientation:UIImageOrientationUp];
imgViewRecorded.image =newImage;
CGImageRelease(imageMasked);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
}在我的特殊情况下,因为这是用于图像初始化的,所以我最终执行了以下操作
UIImage* layerToUpdate= imgViewRecorded.image;
UIGraphicsBeginImageContextWithOptions(layerToUpdate.size, FALSE, 0.0);
NSLog(@"layer created in context");
[layerToUpdate drawInRect:CGRectMake( 0, 0, layerToUpdate.size.width, layerToUpdate.size.height)];
UIFont *font = [UIFont boldSystemFontOfSize:15.0];
[[UIColor whiteColor] set];
for (int i=0; i<self.myProfile.totalSounds; i++)
{
//translate to the center
CGContextTranslateCTM( UIGraphicsGetCurrentContext(), 0.5f * layerToUpdate.size.width, 0.5f * layerToUpdate.size.height ) ;
//rotate with the center of the image as the rotation point
CGContextRotateCTM(UIGraphicsGetCurrentContext(), degrees );
//translate back to original position
CGContextTranslateCTM( UIGraphicsGetCurrentContext(), -0.5f *layerToUpdate.size.width, -0.5f *layerToUpdate.size.height ) ;
NSString* strTextToWrite;
//do set one first
strTextToWrite= ((MyObject*)[arrObjects1 objectAtIndex:soundIndex-1]).soundName;
CGSize size = [strTextToWrite sizeWithFont:font];
[strTextToWrite drawAtPoint:CGPointMake((303-(size.width/2)), 80.0) withFont:font];
//do set 2
strTextToWrite= ((MyObject*)[arrObjects2 objectAtIndex:soundIndex-1]).soundName;
size = [strTextToWrite sizeWithFont:font];
[strTextToWrite drawAtPoint:CGPointMake((303-(size.width/2)), 23.0) withFont:font];
//translate to the center
CGContextTranslateCTM( UIGraphicsGetCurrentContext(), 0.5f * layerToUpdate.size.width, 0.5f * layerToUpdate.size.height ) ;
//rotate back
CGContextRotateCTM(UIGraphicsGetCurrentContext(), -degrees );
//translate back to original position
CGContextTranslateCTM( UIGraphicsGetCurrentContext(), -0.5f *layerToUpdate.size.width, -0.5f *layerToUpdate.size.height ) ;
}
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imgViewRecorded.image = newImage;
UIImage*layerToUpdate=imgViewRecorded.image;
UIGraphicsBeginImageContextWithOptions(layerUpdate.size,FALSE,0.0);
NSLog(@“在上下文中创建的层”);
[layerToUpdate drawInRect:CGRectMake(0,0,layerToUpdate.size.width,layerToUpdate.size.height)];
UIFont*font=[UIFont boldSystemFontOfSize:15.0];
[[UIColor-whiteColor]set];
对于(int i=0;该代码不在线程上,是吗?您是对的,不是。我应该使用performSelectorInBackground还是performSelectorOnMain?我只是问,因为图形上下文只能在main上开始和结束,因为它们不是线程安全的。此外,您的问题似乎不是内存泄漏,而是运行时错误或者,否则它就不会死掉。但是在@try/catch块中的整个调用,并要求对异常进行描述,这可能有助于了解发生了什么。我只是添加了try/catch,不幸的是它从未进入catch中……当我过去有过泄漏时,这是相同的行为。应用程序只是退出。我想它会消失我知道漏洞有多大。你是否使用调试符号进行编译?我昨晚偶然发现了相同的问题,实现了一个类似的解决方案。在我的例子中,我创建了UIImage的扩展以向图像添加文本。当它在循环中运行时,它会耗尽内存。我将修改代码以管理循环外的上下文。感谢这是一个令人担忧的问题。