iphone - WebCore 在 NSOperationQueue 操作中崩溃

标签 iphone objective-c ios ipad

我收到以下错误:

enter image description here

我看不出我的代码有什么问题。这是我的 AHInstagramImageData 中的一些代码

-(id)initWithData:(NSDictionary *)data
{
    self = [super init];
    if (!self) {
        return nil;
    }

for (NSDictionary * comment in imgCmt){
        AHInstagramImageComment * instagramImgComment  = [[AHInstagramImageComment alloc] initWithData:comment];
        [comments insertObject:instagramImgComment atIndex:0];
        [instagramImgComment release];
    }
}

and then on the AHInstagramImageComment I have:

-(id)initWithData:(NSDictionary *)data
{
    self = [super init];
    if (!self) {
        return nil;
    }

    [self calculateCommentsHeight];

    return self;
}

- (void) calculateCommentsHeight
{
    NSString *combinedComment = [NSString stringWithFormat:@"%@ %@", self.username, self.text];
    CGFloat desiredHeight = [combinedComment sizeWithFont:[UIFont fontWithName:@"HelveticaNeue" size:14] constrainedToSize:CGSizeMake(kCommentsMaxWidth, CGFLOAT_MAX) lineBreakMode:UILineBreakModeClip].height;

    if (desiredHeight >= 50){
        commentHeight_ = desiredHeight;
    } else {
        commentHeight_ = 50;
    }

    commentHeight_ += kTimestampMaxHeight;
    commentHeight_ += 5;
}

所以本质上我已经在后台队列中初始化了 AHInstagramImageData:

 [weakSelf.backgroundQueue_ addOperationWithBlock:^{
                             NSArray *arr = [response valueForKey:@"data"];
                             int startingIndex = [[AHImageDataSource sharedDataSource] count];
                             for (NSDictionary * data in arr){
                                 AHInstagramImageData * imgData = [[AHInstagramImageData alloc] initWithData:data];

} }];

这些有错吗?

最佳答案

Core Text 在某种程度上是线程安全的,如下所述:Alternative for sizeWithFont: method

我使用 CoreText 编写了一个 NSString 类别,它包装了 CTFramesetterSuggestFrameSizeWithConstraints并且是线程安全的。可能对找到这篇文章并想要不同解决方案的人有用。这个与 iOS 5 和 iOS 6 兼容,但出于某种原因在 iOS 6 上提供更好的结果。

它会为您返回一个建议的 CGSize,但也会告诉您它能够以何种字体大小容纳多少个字符,并接受约束。

NSString+SizeCalculation.h

@interface NSString (SizeCalculation)

// Uses CoreText instead of UIStringDrawing, since UIStringDrawing is not thread safe
- (CGSize) sizeWithFontName:(NSString*)fontName constraints:(CGSize)constraint targetSize:(CGFloat)target minimumSize:(CGFloat)minimum optimalFontSize:(CGFloat*)optimal fitRange:(NSRange*)range;

@end

NSString+SizeCalculation.m

#import <CoreText/CoreText.h>
@implementation NSString (SizeCalculation)

- (CGSize) sizeWithFontName:(NSString*)fontName constraints:(CGSize)constraint targetSize:(CGFloat)target minimumSize:(CGFloat)minimum optimalFontSize:(CGFloat*)optimal fitRange:(NSRange*)range
{
    CFRange fitRange;
    CGSize suggestion;
    do {
        id font = (__bridge_transfer id) CTFontCreateWithName((__bridge CFStringRef) fontName, target, NULL);
        NSAttributedString *string = [[NSAttributedString alloc] initWithString:self attributes:@{(id)kCTFontAttributeName:font}];
        id framesetter = (__bridge_transfer id) CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) string);
        suggestion = CTFramesetterSuggestFrameSizeWithConstraints((__bridge CTFramesetterRef)(framesetter), CFRangeMake(0, self.length), nil, constraint, &fitRange);
    } while (fitRange.length < self.length && target > minimum && (target -= 2.0f));

    if (optimal != NULL) *optimal = target;
    if (range != NULL) *range = NSMakeRange(fitRange.location,fitRange.length);
    return suggestion;
}

@end

你可以这样使用它。

 CGFloat fontSize = 24.0f;
 NSRange range;
 NSString *title = @"Hello World";
 CGSize suggestedSize = [title sizeWithFontName:@"HelveticaNeue-Bold" constraints:CGSizeMake(200.0f,35.0f) targetSize:fontSize minimumSize:16.0f optimalFontSize:&fontSize fitRange:&range];

suggestedSize 然后包含一个 CGSize,它将适合字体为 fontSize 的标题。 range 还会告诉您在达到最小尺寸之前它能够容纳多少个字符。如果您不在乎,可以将 NULL 传递给最佳尺寸和适合范围。

附言我尝试重新使用 CoreText 部分,但它没有用,所以需要重新初始化,但它仍然非常快,而且因为你在后台线程上运行,所以它会很好。

P.p.s 我写了一个类似的方法,如果你只需要获取单行文本的高度,性能会更好。 https://gist.github.com/duedal/4742069

关于iphone - WebCore 在 NSOperationQueue 操作中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12505558/

相关文章:

iphone - 了解坐标, View , subview 和方向更改

objective-c - memcpy 问题和访问错误

ios - 添加应用程序组而不在 Xcode 上添加开发者帐户?

iphone - 在应用程序中查看核心数据

iphone - Objective-C 的其他选择

ios - 重新启动应用程序或关闭应用程序后,计划的 UILocalNotification 不会触发

objective-c - 为什么 NSURLSession 不使用我配置的 NSURLCache?

c++ - 在 iOS : "Undefined symbols" and "ld: warning: ignoring file" 中为 C++ 编译 Protobuf 时出错

iphone - 核心数据 : Why removing the sqlite file did not clear all the previous data insert immediately?

javascript - 在智能手机上加载时禁用 JavaScript 脚本