0

jsonファイルからUIImage(Xcodeのテーブルセル)に画像を読み込むのに少し問題があります。サーバーからNSArrayに画像を読み込んでから、テーブルビューのUIImageセルにデータを入力しようとしました。ここで欠けているものはありますか?

画像はSQLサーバー上にあります。

助けてくれてありがとう。

これがPHPからXcodeへのサーバー出力です。(表紙画像)

(
    "13497074790148.jpeg",
    "13494650900147.png",
    "13494606630147.png",
    "13494605220147.jpeg",
    "13494602920147.jpeg",
    "13494601850147.jpeg",
    "13491916300147.jpeg"
)

これがXcodeのコードです

NSArray *itemsimages = [[NSArray alloc]initWithArray:[results valueForKeyPath:@"cover_image"]];
self.itemImages = itemsimages;

これがテーブルセルのコードです

UIImage *imageitm = [UIImage imageNamed: [self.itemImages objectAtIndex: [indexPath row]]];
cell.itmImage.image = imageitm;
return cell;
4

3 に答える 3

1

これらの画像はローカルに保存されていないため、表示する画像はありません。SDWebImageを使用して、リモートロケーションからの非同期画像読み込み+キャッシュメカニズムを提供することをお勧めします。

于 2012-10-26T14:53:51.497 に答える
1
-(void) viewDidLoad
{

NSURL *url = [NSURL URLWithString:@"YOUR URL"];

NSData *data = [NSData dataWithContentsOfURL:url];

    NSError *error;

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSMutableArray *img = [[NSMutableArray alloc]init];
    NSArray *websiteDetails = (NSArray *) [json objectForKey:@"logos"];

    for(int count=0; count<[websiteDetails count]; count++)
    {
        NSDictionary *websiteInfo = (NSDictionary *) [websiteDetails objectAtIndex:count];
        imagefile = (NSString *) [websiteInfo objectForKey:@"image_file"];
        if([imagefile length]>0)
        {
            NSLog(@"Imagefile URL is: %@",imagefile);
            [img addObject:imagefile];
        }
    }
    //NSarray listofURL
    listofURL = img;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //this will start the image loading in bg
    dispatch_async(concurrentQueue, ^{

        NSURL *url = [NSURL URLWithString:[listofURL objectAtIndex:indexPath.row]];
        NSData *image = [[NSData alloc] initWithContentsOfURL:url];

        //this will set the image when loading is finished
        dispatch_async(dispatch_get_main_queue(), ^{

            cell.imageView.image = [UIImage imageWithData:image];
        });
    });
    }
    return cell;
}
于 2014-03-10T09:17:40.053 に答える
0

jsonレスポンスに適切なURLを含める必要があります。または、URLの共通部分をコード自体に保存し、後でサーバーから返された画像名を追加することができます。同じ条件で次のようにしました

    __autoreleasing NSError* error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSDictionary *dict = ((NSDictionary *) result)[@"result"];

    NSString *url = dict[@"imageURL"];

    NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
    UIImage *image = [[UIImage alloc] initWithData:imageData];

    [_buttonImageView setImage:image forState:UIControlStateNormal];

ここで、dataはサーバーから返された応答です。

于 2012-10-26T15:12:22.453 に答える