无法发布Quartz 2D和Core Text创建的图像 [英] Unable to Release Quartz 2D and Core Text created Images

查看:57
本文介绍了无法发布Quartz 2D和Core Text创建的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在删除通过Quartz 2D和Core Text创建的OpenGL ES纹理时遇到问题,如下所示:

I am having a problem deleting my OpenGL ES textures are created via Quartz 2D and Core Text as shown below :

- (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;

}

但是,当我尝试按以下方式在viewWillDisappear中删除纹理时,我可以看到(在乐器中)内存未释放:

However when I try to delete the textures as follows in viewWillDisappear, I can see (in instruments) that the memory is not released :

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

我认为上面的代码中某些地方保留了纹理.谁能建议在哪里?

I think that something is being retained in the texture somewhere in the code above. Can anyone suggest where ?

推荐答案

据我所知,如果您使用的是ARC,则代码中没有内存泄漏.但是,从drawTexture返回的 UIImage 可能会被其他一些对象或自动释放池保留.

As far as I can see, there's no memory leak in your code if you're using ARC. However, the UIImage returned from drawTexture may be retained by some other objects or by the auto release pool.

要检查是否存在这种情况,可以将UIImage子类化,例如说UIDebugImage,重写dealloc方法并在其中设置一个断点以查看是否被调用.

To check if this is the case, you may subclass UIImage, say UIDebugImage, override the dealloc method and set a breakpoint there to see if it's called.

这篇关于无法发布Quartz 2D和Core Text创建的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆