我正在继承一个UILabel而不是使用drawRect我在背景中将文本呈现给UIImage ..这是代码:
- (UIImage *)imageForText
{
UIGraphicsBeginImageContextWithOptions(self.frameSize, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGAffineTransform transform = [self _transformForCoreText];
CGContextConcatCTM(ctx, transform);
if (nil == self.textFrame) {
CFAttributedStringRef attributedString = (__bridge CFAttributedStringRef)attributedStringWithLinks;
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attributedString);
CGMutablePathRef path = CGPathCreateMutable();
// We must tranform the path rectangle in order to draw the text correctly for bottom/middle
// vertical alignment modes.
CGPathAddRect(path, &transform, rect);
if (nil != self.shadowColor) {
CGContextSetShadowWithColor(ctx, self.shadowOffset, self.shadowBlur, self.shadowColor.CGColor);
}
self.textFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CGPathRelease(path);
CFRelease(framesetter);
}
// Draw the tapped link's highlight.
if ((nil != self.touchedLink || nil != self.actionSheetLink) && nil != self.highlightedLinkBackgroundColor) {
[self.highlightedLinkBackgroundColor setFill];
NSRange linkRange = nil != self.touchedLink ? self.touchedLink.range : self.actionSheetLink.range;
CFArrayRef lines = CTFrameGetLines(self.textFrame);
CFIndex count = CFArrayGetCount(lines);
CGPoint lineOrigins[count];
CTFrameGetLineOrigins(self.textFrame, CFRangeMake(0, 0), lineOrigins);
for (CFIndex i = 0; i < count; i++) {
CTLineRef line = CFArrayGetValueAtIndex(lines, i);
CFRange stringRange = CTLineGetStringRange(line);
NSRange lineRange = NSMakeRange(stringRange.location, stringRange.length);
NSRange intersectedRange = NSIntersectionRange(lineRange, linkRange);
if (intersectedRange.length == 0) {
continue;
}
CGRect highlightRect = [self _rectForRange:linkRange inLine:line lineOrigin:lineOrigins[i]];
if (!CGRectIsEmpty(highlightRect)) {
CGFloat pi = (CGFloat)M_PI;
CGFloat radius = 5.0f;
CGContextMoveToPoint(ctx, highlightRect.origin.x, highlightRect.origin.y + radius);
CGContextAddLineToPoint(ctx, highlightRect.origin.x, highlightRect.origin.y + highlightRect.size.height - radius);
CGContextAddArc(ctx, highlightRect.origin.x + radius, highlightRect.origin.y + highlightRect.size.height - radius,
radius, pi, pi / 2.0f, 1.0f);
CGContextAddLineToPoint(ctx, highlightRect.origin.x + highlightRect.size.width - radius,
highlightRect.origin.y + highlightRect.size.height);
CGContextAddArc(ctx, highlightRect.origin.x + highlightRect.size.width - radius,
highlightRect.origin.y + highlightRect.size.height - radius, radius, pi / 2, 0.0f, 1.0f);
CGContextAddLineToPoint(ctx, highlightRect.origin.x + highlightRect.size.width, highlightRect.origin.y + radius);
CGContextAddArc(ctx, highlightRect.origin.x + highlightRect.size.width - radius, highlightRect.origin.y + radius,
radius, 0.0f, -pi / 2.0f, 1.0f);
CGContextAddLineToPoint(ctx, highlightRect.origin.x + radius, highlightRect.origin.y);
CGContextAddArc(ctx, highlightRect.origin.x + radius, highlightRect.origin.y + radius, radius,
-pi / 2, pi, 1);
CGContextFillPath(ctx);
}
}
CTFrameDraw(self.textFrame, ctx);
CGContextRestoreGState(ctx);
self.renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return self.renderedImage;
}
现在,这是我在仪器上的分配工具上获得的快照。
我不确定如何更深入地了解这一点,但似乎图像没有被释放并保存在内存中并且随着我滚动UIScrollView而增加,并且越来越多地调用此方法。有任何想法吗?上面的代码有问题吗?我确实使用泄漏进行了剖析,没有发现漏水迹象......
修改
我添加了关于我如何调用imageForText的代码:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[self parseTagsInComment];
[self.commentsText_ setLinkColor:self.textColor_];
[weakSelf.commentsText_ imageForText];
UIImage *renderedImage = [weakSelf.commentsText_ imageForText];
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.commentTextImageView_ setImage:renderedImage];
[weakSelf.commentTextImageView_ setAlpha:0.0];
[UIView animateWithDuration:0.3 animations:^{
[weakSelf.commentTextImageView_ setAlpha:1.0];
}];
});
[pool release];
});
我甚至尝试过这样做:
[self.commentsText_ imageForText];
然而记忆仍然飙升
答案 0 :(得分:0)
我们需要看看你是如何打电话给imageForText
告诉你的,但是你可能会认为你是双重保留来自UIGraphicsGetImageFromCurrentImageContext()
的图像。您将self.renderedImage
设置为UIImage
UIGraphicsGetImageFromCurrentImageContext()
,并返回图像。我假设renderedImage
是strong
属性,因此它会保留一次。返回的图像可能设置在局部变量中或以其他方式保留在其他位置。 imageForText
更典型的模式不具有保留图像的副作用,如果您需要将图像保留在属性中,则将其调用如下:
self.renderedImage = [self imageForText];
// Do whatever you want with self.renderedImage ...
另一个潜在的问题是,当你说你“在背景中将文本呈现给UIImage”时,希望你不要在后台线程中表示。像UIGraphicsBeginImageContextWithOptions()
这样的UIKit函数只能从应用程序的主线程中调用。
答案 1 :(得分:0)
您应该在辅助线程中显式创建位图上下文。从后台线程使用CoreGraphics没有问题,但是不要从辅助线程调用UIKit方法,这将导致很难调试问题。苹果文档(如UIFont)中明确提到了一些例外,但一般规则是不使用UIKit函数,因为它们除了主线程外不能正常工作。另外,不要调用self.frameSize等访问方法,因为这会调用UIKit。
因为您正在使用CoreText,所以您可能也会因此内存泄漏而感到沮丧memory-usage-grows-with-ctfontcreatewithname-and-ctframesetterref。