1

AFNetworking を使用して、JSON を解析してアプリに渡しています (Rails をバックエンドとして使用しています)。現在、私のアプリは非常に遅いので、よりスムーズにする方法を見つけようとしています。アプリを最初にロードすると、データが読み込まれるまでに数秒かかります (Nav 項目と白いページが表示され、数秒後に「投稿」が表示されます)。

コレクション ビュー コントローラー

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.upcomingReleases = [[NSMutableArray alloc] init];

    [self makeReleasesRequests];

    [self.collectionView registerClass:[ReleaseCell class] forCellWithReuseIdentifier:@"ReleaseCell"];
}

-(void)makeReleasesRequests
{
    NSURL *url = [NSURL URLWithString:@"http://www.soleresource.com/upcoming.json"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"@");

        self.upcomingReleases = [responseObject objectForKey:@"upcoming_releases"];

        [self.collectionView reloadData];

    } failure:nil];

    [operation start];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.upcomingReleases count];
}

#pragma mark - Show upcoming release shoe

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"Cell";

    ReleaseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    NSDictionary *upcomingReleaseDictionary = [self.upcomingReleases objectAtIndex:indexPath.row];

    NSString *thumbURL = nil;

    cell.release_name.text = [NSString stringWithFormat:@"%@ — $%@",[upcomingReleaseDictionary objectForKey:@"release_name"], [upcomingReleaseDictionary objectForKey:@"release_price"]];

    if ([upcomingReleaseDictionary[@"images"] isKindOfClass:[NSArray class]] && [upcomingReleaseDictionary[@"images"] count]) {
        thumbURL = upcomingReleaseDictionary[@"images"][0][@"image_file"][@"image_file"][@"thumb"][@"url"];
        if (thumbURL)
        {
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:thumbURL]];
            UIImage *image = [UIImage imageWithData:imageData];
            cell.thumb.image = image;
        }
    }
    else {
        cell.thumb.image = [UIImage imageNamed:@"air-jordan-5-fear.png"];
    }

    return cell;
}

私の各投稿には、テキスト文字列と画像があります。すぐに表示されるようにテキストを読み込んでから画像を読み込む方法はありますか? または、アプリの読み込み速度を高速化する別の方法がありますか (おそらく、最初に特定の投稿を読み込んでから、残りの投稿を読み込みます。ユーザーが下にスクロールするまで表示されない投稿)。

ありがとう。

4

2 に答える 2

0

サーバーから来るときは、画像を遅延して非同期にロードする必要があります(メインスレッドをブロックしないでください)。(AFNetworking には既に UIImageView のキャッシング カテゴリ メソッドがあります。(詳細については、こちらを参照してください)

    if (thumbURL)
    {
        [cell.thumb setImageWithURL:[NSURL URLWithString:thumbURL] placeholderImage:[UIImage imageNamed:@"air-jordan-5-fear.png"]];
    }

編集-

UIKit+AFNetworkingフォルダーをプロジェクトと#import "UIKit+AFNetworking.h".m ファイルにプルするようにしてください。完全な AFNetworking をダウンロードするためのリンクはここにあり、この質問に固有のドキュメントはここにあります

于 2013-10-29T19:59:23.643 に答える