EXC_BAD Access at CFAttributedStringReplaceString (currentText,CFRangeMake(0, 0), string);

StackOverflow https://stackoverflow.com/questions/23675040

  •  23-07-2023
  •  | 
  •  

سؤال

I generated pdf this tutorial. I need to generate the pdf from two different view controllers. When I push from one view controller to get in the VC where I generate the pdf, it does not crash. But when I push from 2nd view controller to get in the VC where I generate the pdf, it gives me EXC_BAD Access

Following is my code.

- (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);
}
هل كانت مفيدة؟

المحلول

I don't know if this was a good practise or not, but the following code worked for me. I handled string condition and checked it with NULLand skipped the statement as follows.

- (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);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top