Objective c 无法释放Quartz 2D和Core文本创建的图像
我在删除通过Quartz 2D和Core文本创建的OpenGL ES纹理时遇到问题,如下所示:Objective c 无法释放Quartz 2D和Core文本创建的图像,objective-c,core-graphics,automatic-ref-counting,quartz-2d,core-text,Objective C,Core Graphics,Automatic Ref Counting,Quartz 2d,Core Text,我在删除通过Quartz 2D和Core文本创建的OpenGL ES纹理时遇到问题,如下所示: - (void)drawText:(CGContextRef)contextP startX:(float)x startY:(float) y withText:(NSString *)standString { CGContextTranslateCTM(contextP, 0, (bottom-top)*2); CGContextScaleCTM(contextP, 1.0, -
- (void)drawText:(CGContextRef)contextP startX:(float)x startY:(float)
y withText:(NSString *)standString
{
CGContextTranslateCTM(contextP, 0, (bottom-top)*2);
CGContextScaleCTM(contextP, 1.0, -1.0);
CGRect frameText = CGRectMake(1, 0, (right-left)*2, (bottom-top)*2);
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:standString];
[attrString addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Helvetica-Bold" size:12.0]
range:NSMakeRange(0, attrString.length)];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(attrString));
struct CGPath * p = CGPathCreateMutable();
CGPathAddRect(p, NULL, frameText);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,0), p, NULL);
CTFrameDraw(frame, contextP);
CFRelease(framesetter);
CFRelease(frame);
CGPathRelease(p);
standString = nil;
attrString = nil;
}
- (UIImage *)drawTexture : (NSArray *)verticesPassed : (UIColor *)statusColour : (NSString *)standString {
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGRect shp = [self boundFromFrame:verticesPassed];
CGContextRef conPattern = CGBitmapContextCreate(NULL,
shp.size.width*sceneScalar,
shp.size.height*sceneScalar,
8,
0,
rgbColorSpace,
(CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(rgbColorSpace);
CGContextSetLineWidth(conPattern, 2);
CGContextSetStrokeColorWithColor(conPattern, [UIColor blackColor].CGColor);
Line * start = [sortedVertices objectAtIndex:0];
StandPoint * startPoint = start.origin;
CGContextMoveToPoint(conPattern, ([startPoint.x floatValue]-shp.origin.x)*sceneScalar , ([startPoint.y floatValue]-shp.origin.y)*sceneScalar);
for (Line * vertice in sortedVertices) {
StandPoint * standPoint = vertice.origin;
CGContextAddLineToPoint(conPattern, ([standPoint.x floatValue]-shp.origin.x)*sceneScalar, ([standPoint.y floatValue]-shp.origin.y)*sceneScalar);
}
CGContextAddLineToPoint(conPattern, ([startPoint.x floatValue]-shp.origin.x)*sceneScalar , ([startPoint.y floatValue]-shp.origin.y)*sceneScalar);
CGContextSetFillColorWithColor(conPattern, statusColour.CGColor);
CGContextDrawPath(conPattern, kCGPathFillStroke);
[self drawText:conPattern startX:0 startY:20 withText:standString];
CGImageRef cgImage = CGBitmapContextCreateImage(conPattern);
UIImage *imgPattern = [[UIImage alloc]initWithCGImage:cgImage];
//UIImageWriteToSavedPhotosAlbum(imgPattern, nil, nil, nil); Uncomment if you need to view textures (photo album).
self.standHeight = (top - bottom)*sceneScalar;
self.standWidth = (right - left)*sceneScalar;
self.xOffset = [startPoint.x floatValue]*sceneScalar;
self.yOffset = (-[startPoint.y floatValue]*sceneScalar)+standHeight;
CFRelease(cgImage);
CGContextRelease(conPattern);
return imgPattern;
}
但是,当我尝试在VIEWWILLOVERE中删除以下纹理时,我可以看到(在仪器中)内存没有释放:
for (PolygonObject * stand in standArray) {
GLuint name = stand.textureInfo.name;
// delete texture from opengl
glDeleteTextures(1, &name);
// set texture info to nil
stand.textureInfo = nil;
}
我认为在上面代码的某个地方纹理中保留了一些东西。有人能告诉我在哪里吗?据我所知,如果使用ARC,代码中不会出现内存泄漏。但是,从drawTexture返回的UIImage可能会被其他一些对象或自动释放池保留
若要检查是否存在这种情况,您可以将UIImage子类化,例如UIDebugImage,覆盖dealloc方法并在那里设置断点,以查看是否调用了它。检查您的CTFramesetterRef,使用answerlooks释放,就像OpenGL中没有的问题一样,尝试创建图像并删除,而不创建纹理。并在-[UIImage imageWithCGImage:]上替换