0

次のエラーが発生します。

ここに画像の説明を入力してください

コードのどこが悪いのかわからない。これが私の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];

}}];

これらのいずれかが間違っていますか?

4

3 に答える 3

4

ここで説明されているように、コアテキストはややスレッドセーフです:sizeWithFontの代替:メソッド

CoreTextを使用して、CTFramesetterSuggestFrameSizeWithConstraintsをラップし、スレッドセーフなNSStringカテゴリを作成しました。この投稿を見つけて別の解決策を求めている人に役立つ可能性があります。これはiOS5とiOS6の両方と互換性がありますが、何らかの理由でiOS6でより良い結果が得られます。

提案された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には、フォントfontSizeのタイトルに適合するCGSizeが含まれます。また、範囲は、最小サイズに達する前に何文字を収めることができたかを示します。気にしない場合は、NULLを最適なサイズと適合範囲に渡すことができます。

PS CoreTextパーツを再利用しようとしましたが、機能しなかったため、再初期化する必要がありますが、それでもかなり高速であり、バックグラウンドスレッドで実行しているので問題ありません。

Pps 1行のテキストの高さを取得するだけでよい場合は、パフォーマンスが向上した同様のメソッドを作成しました。https://gist.github.com/duedal/4742069

于 2013-01-24T19:18:37.973 に答える
3

私は以前これと同様の問題を抱えていました。現在、バックグラウンドスレッドで、またはを介して実行できないバグが[NSString sizeWithFont:]あります。[NSString drawInRect:]NSOperations

このリンクを参照してください: UIKit文字列描画メソッドのバグ?

いくつかのオプションがあります。

  1. sizeWithFont:と一緒に使用しないでくださいNSOperations
  2. の最大操作数NSOperationQueueを1に設定します。

    [queue setMaxConcurrentOperations:1];

  3. NSAttributedStrings独自の" sizeWithFont"メソッドを持つCoreTextとを使用します。CTFramesetterSuggestFrameSizeWithConstraints

  4. [NSOperationQueue mainQueue];メインスレッドで操作を実行するために使用します。
于 2012-09-20T04:00:24.133 に答える
0

WebCore / WebKitは完全にスレッドセーフではないため、メインスレッドで実行する必要があります。問題は、フォントと文字列の操作でWebKitが隠されていることです。

メインスレッドで文字列描画(サイズ設定を含む)コードを呼び出すと、クラッシュが停止するはずです。

于 2012-09-20T04:04:50.710 に答える