我生成了pdf this tutorial。我需要从两个不同的视图控制器生成pdf。当我从一个视图控制器推进到我生成pdf的VC时,它不会崩溃。但是当我从第二视图控制器推进到我生成pdf的VC时,它给了我EXC_BAD Access
以下是我的代码。
- (void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect fontSize:
(float)sizeofFont
{
NSUInteger length = [textToDraw length];
CFStringRef string = (__bridge CFStringRef) textToDraw;
CFMutableAttributedStringRef currentText =
CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (currentText,CFRangeMake(0, 0), string); **//EXC_BAD_ACCESS**
// CFAttributedStringReplaceString (currentText,CFRangeMake(0, length-1), string);
CTFontRef font = CTFontCreateWithName((CFStringRef)@"UniversLTStd-BoldCn",
sizeofFont, nil);
CFAttributedStringSetAttribute(currentText,CFRangeMake(0,
length),kCTFontAttributeName,font);
if (self.flagToSetBlueColoredText == NO)
{
CFAttributedStringSetAttribute(currentText,CFRangeMake(0, length),kCTForegroundColorAttributeName,[UIColor blackColor].CGColor);
}
else if(self.flagToSetBlueColoredText == YES)
{
CFAttributedStringSetAttribute(currentText,CFRangeMake(0, length),kCTForegroundColorAttributeName,[UIColor blueColor].CGColor);
}
CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString(currentText);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange,
framePath, NULL);
CGPathRelease(framePath);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CTFrameDraw(frameRef, currentContext);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);
CFRelease(frameRef);
//CFRelease(currentText);
CFRelease(framesetter);
//CFRelease(stringRef);
}
答案 0 :(得分:0)
我不知道这是不是一个好习惯,但以下代码对我有用。我处理了字符串条件并使用NULL
进行了检查,并跳过了以下语句。
- (void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect fontSize:
(float)sizeofFont
{
NSInteger length = [textToDraw length];
CFStringRef string = (__bridge CFStringRef) textToDraw;
CFMutableAttributedStringRef currentText =
CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
if (string == NULL)
{
//CFAttributedStringReplaceString(nil ,range, nil);
}
else
{
CFAttributedStringReplaceString(currentText,CFRangeMake(0, 0), string); //EXC_BAD_ACCESS due to nil values of string and currentText
}
CTFontRef font = CTFontCreateWithName((CFStringRef)@"UniversLTStd-BoldCn",
sizeofFont, nil);
CFAttributedStringSetAttribute(currentText,CFRangeMake(0,
length),kCTFontAttributeName,font);
if (self.flagToSetBlueColoredText == NO)
{
CFAttributedStringSetAttribute(currentText,CFRangeMake(0, length),kCTForegroundColorAttributeName,[UIColor blackColor].CGColor);
}
else if(self.flagToSetBlueColoredText == YES)
{
CFAttributedStringSetAttribute(currentText,CFRangeMake(0, length),kCTForegroundColorAttributeName,[UIColor blueColor].CGColor);
}
CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString(currentText);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange,
framePath, NULL);
CGPathRelease(framePath);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CTFrameDraw(frameRef, currentContext);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);
CFRelease(frameRef);
//CFRelease(currentText);
CFRelease(framesetter);
//CFRelease(stringRef);
}