2013-01-16 157 views
2

我有以下代碼:CFMutableAttributedString設備上(EXC_BAD_ACCESS)運行時崩潰

NSString *name = @"some text goes in here"; 

     CFStringRef string = (__bridge CFStringRef) self.highlightItem_.comment; 
     CFMutableAttributedStringRef comment = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 
     CFAttributedStringReplaceString (comment ,CFRangeMake(0, 0), string); 

     CGColorRef blue =[UIColor colorWithRed:128/255.f green:203/255.f blue:255/255.f alpha:1.0].CGColor; 
     CGColorRef gray =[UIColor colorWithWhite:153/255.f alpha:1.0].CGColor; 

     CFAttributedStringSetAttribute(comment, CFRangeMake(0, [name length]),kCTForegroundColorAttributeName, blue); 
     CFAttributedStringSetAttribute(comment, CFRangeMake([name length], [self.highlightItem_.comment length] - [name length]),kCTForegroundColorAttributeName, gray); 

     CTFontRef nameFont = CTFontCreateWithName((__bridge CFStringRef)kProximaNovaBold, 15.0f, nil); 
     CFAttributedStringSetAttribute(comment,CFRangeMake(0, [name length]),kCTFontAttributeName,nameFont); 

     CTFontRef commentFont = CTFontCreateWithName((__bridge CFStringRef)kProximaNova, 15.0f, nil); 
     CFAttributedStringSetAttribute(comment, CFRangeMake([name length], [self.highlightItem_.comment length] - [name length]),kCTFontAttributeName,commentFont); 

     CGContextSaveGState(context); 
     CGRect captionFrame = CGRectMake(0, 0, rect.size.width - 80, commentHeight); 
     CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(comment); 
     CGMutablePathRef captionFramePath = CGPathCreateMutable(); 
     CGPathAddRect(captionFramePath, NULL, captionFrame); 

     CTFrameRef mainCaptionFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), captionFramePath, NULL); 

     CGContextRef context = UIGraphicsGetCurrentContext(); 
     CGContextSetTextMatrix(context, CGAffineTransformIdentity); 
     CGContextTranslateCTM(context, 70, self.imageHeight_ + commentHeight + 20); 
     CGContextScaleCTM(context, 1.0, -1.0); 

     CTFrameDraw(mainCaptionFrame, context); 
     CGContextRestoreGState(context); 

     CFRelease(framesetter); 
     CGPathRelease(captionFramePath); 

我不知道爲什麼,當我在模擬器上它的所有工作運行良好,但是當我在設備上運行,它在這條線上崩潰:

CFAttributedStringSetAttribute(comment, CFRangeMake(0, [name length]),kCTForegroundColorAttributeName, blue); 

任何想法爲什麼?

+0

我刪除了我的答案,我沒有看到你已經從UIColor CGColor屬性。 –

+0

iOS在模擬器或設備上運行的是什麼? – holex

回答

1

所以問題是我沒有保留我的CGColor。如果我把CGColorRetain然後它的工作

0

不應該

CFMutableAttributedStringRef comment = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 
CFAttributedStringReplaceString (comment ,CFRangeMake(0, 0), string); 

閱讀

CFMutableAttributedStringRef comment = CFAttributedStringCreateMutable(
    kCFAllocatorDefault, 
    CFAttributedStringGetLength(string)); 
CFAttributedStringReplaceString(comment, 
    CFRangeMake(0, CFAttributedStringGetLength(string)), 
    string); 

相關問題