我收到以下错误:
我看不出我的代码有什么问题。这是我的 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/