0

NSArray画像があります:

2012-08-15 10:12:39.687 test[4200:11103] (
    "http://192.168.1.165/Images/Demo/1.jpg",
    "http://192.168.1.165/Images/Demo/0.jpg",
    "http://192.168.1.165/Images/Demo/0.jpg",
    "http://192.168.1.165/Images/Demo/2.jpg",
    "http://192.168.1.165/Images/Demo/2.jpg",
    "http://192.168.1.165/Images/Demo/2.jpg",
    "http://192.168.1.165/Images/Demo/2.jpg",
    "http://192.168.1.165/Images/Demo/1.jpg",
    "http://192.168.1.165/Images/Demo/1.jpg",
    "http://192.168.1.165/Images/Demo/1.jpg" )

コード .m:

dispatch_async(htvque, ^{
        NSData* data = [NSData dataWithContentsOfURL: tophotfilm];

        NSError* error;

        json = [NSJSONSerialization JSONObjectWithData:data
                                               options:kNilOptions 
                                                 error:&error];

        NSDictionary *list = [json objectForKey:@"List"];


        NSMutableArray *arrPoster =[[NSMutableArray alloc]init];
        for(NSString *po in list)
        {
            NSString *poster = [po valueForKey:@"Poster"];
            [arrPoster addObject:poster];

        }
        myArray = [NSArray arrayWithArray:arrPoster];
        NSArray *colors = [[NSArray alloc] initWithObjects:[UIColor redColor], 
                           [UIColor greenColor], [UIColor magentaColor],
                           [UIColor blueColor], [UIColor orangeColor], 
                           [UIColor brownColor], [UIColor grayColor], nil];
            //NSLog(@"%@",colors);
            NSLog(@"%@",myArray);
        for (int i = 0; i < myArray.count; i++) {
                //NSLog(@"%@",myArray.count);
            NSString *imageName = [NSString stringWithFormat:@"%@", i];
            UIImage *image = [UIImage imageNamed:imageName];
            UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

            CGRect rect = imageView.frame;

            rect.origin.x = self.scrollView.frame.size.width * i;
            rect.origin.y = 0;
            rect.size = scrollView.frame.size;

            [self.scrollView addSubview:imageView];

        }

        scrollView.pagingEnabled = YES;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width *
        myArray.count,scrollView.frame.size.height);
        pageControl.currentPage = 0;
        pageControl.numberOfPages = myArray.count;
        [super viewDidLoad];
    });

NSArray の色では問題なく動作しますが、NSArray の画像にエラーがあります。


scrollview は listimage を表示しません。最初の画像を表示します。理由は誰にもわかります...

dispatch_async(dispatch_get_main_queue(), ^{ for (int i = 0; i < myArray.count; i++) { //NSString *imageName = [NSString stringWithFormat:@"%@",[myArray objectAtIndex:i]];

                NSURL *urlImage = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[myArray objectAtIndex:i]]];
                //NSLog(@"%@",urlImage);
                UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:urlImage]];
                UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

                CGRect rect = imageView.frame;
                rect.size.height = imgHeight;
                rect.size.width = imgWidth;
                imageView.frame = rect;
                imageView.tag = i;

                //rect.size = scrollView.frame.size;
                [scrollView addSubview:imageView];
            }

            scrollView.pagingEnabled = YES;
            scrollView.showsHorizontalScrollIndicator = NO;
            scrollView.showsVerticalScrollIndicator = NO;
            scrollView.contentSize = CGSizeMake(scrollView.frame.size.width *

myArray.count,scrollView.frame.size.height); pageControl.currentPage = 0; pageControl.numberOfPages = myArray.count; });

4

2 に答える 2

0

このコード行を確認してください

NSString *imageName = [NSString stringWithFormat:@"%@", i];

イメージ名は、ログに出力されるイメージ名ではなく、1、2 などになります。

それは次のようなものでなければなりません

[myArray objectATIndex:i];

于 2012-08-15T04:05:12.250 に答える
0

imageNamed:プロジェクト内の画像にのみ使用できるものを使用しています。質問を理解した場合、サーバーからそれらにアクセスしようとしています。このUIImage方法を試してください:imageWithData:

NSURL *url = [NSURL URLWithString:[myArray objectAtIndex:i]];
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
于 2012-08-15T14:40:35.953 に答える