1

我创建了 2 个类(RecipeViewController& DetailViewController) ,RecipeViewTableViewController向我展示了 4 个单元格(书名),它DetailView可以向我展示任何一本书的许多图像。UIViewControllerUIScroll

我的问题是当我单击任何单元格并转到下一页时UIScroller不向我显示图像。

这是我的代码:

食谱视图控制器.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int x = (indexPath.row)+1;
    NSLog(@"x : %d",x);
    DetailViewController *obj = [[DetailViewController alloc]init];
    obj.yourValue = [NSString stringWithFormat:@"%d",(indexPath.row)+1];
    [self presentModalViewController:obj animated:YES];
    NSLog(@"yourValue1 : %@",obj.yourValue);
}  

细节视图控制器.m

#import "DetailViewController.h"
#import "RecipeViewController.h"


@implementation DetailViewController
@synthesize scroller,yourValue;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

   //m1 variable tell me in tableview which choice book 
    int m1 = [self.yourValue integerValue];

    NSLog(@"m1 : %d",m1);

    //this code give me number of images (pages of book)
    NSString *numberbook = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.102/mamal/book.php?info=0&b=%d",m1]]];

    NSInteger numbook = [numberbook integerValue];

    NSLog(@"%d",numbook);

    for (int i = 1; i <= numbook; i++)
    {
        //this code for recive images from specific book
        NSData *dat = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.102/mamal/book.php?p=%d&b=%d",i,m1]]];
        NSLog(@"%@",dat);

        UIImageView *imagen = [[UIImageView alloc] initWithImage:[UIImage imageWithData:dat]];

        imagen.frame = CGRectMake((i-1)*320, 0, 320, 460);

        [scroller addSubview:imagen];
    }
    scroller.delegate = self;
    scroller.contentSize = CGSizeMake(320*numbook, 460);
    scroller.pagingEnabled = YES;

}

@end

对不起我的英语不好!!!

4

2 回答 2

0

如果你真的想使用 [NSData dataWithContentsOfURL:]; 将此添加到您的循环、chnage url 和 imageView 框架中。

for (int i = 1; i <= numbook; i++)
{
    UIImageView *yourImage=[[UIImageView alloc]init];
    yourImage.frame = CGRectMake((i-1)*320, 0, 320, 460);
    [scroller addSubview:imagen];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

    dispatch_async(queue, ^{


        NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.102/mamal/book.php?p=%d&b=%d",i,m1]];

        NSData *imgData = [NSData dataWithContentsOfURL:url];

        dispatch_sync(dispatch_get_main_queue(), ^{

            UIImage *downloadedImage = [UIImage imageWithData:imgData];

            yourImage.image = downloadedImage;  
            if(yourImage.image==nil)
            {
                [yourImage setImage:@"placeholder.png"];

            }
        });
    });

}
于 2013-04-04T08:58:48.920 回答
0

嘿,你在主线程中做所有事情,可能会有延迟或其他一些问题,通常不建议这样做,你能利用 NickLockWoods 实现来异步加载图像吗

https://github.com/nicklockwood/AsyncImageView

是一个非常好的UIImageView类,只需要导入AsyncImageView即可使用

[imageView setImageURL:@"ur url"];

它异步加载所有图像,并且有很多自定义项。

在你的情况下:

for (int i = 1; i <= numbook; i++)
{
UIImageView *imagen = [[UIImageView alloc] initWithFrame: CGRectMake((i-1)*320, 0, 320, 460)];
[imagen setImageURL: [NSString stringWithFormat:@"192.168.1.102/mamal/book.php?p=%d&b=%d",i,m1]]]; 
[scroller addSubview:imagen]; 
}

您正在主线程中执行所有操作,这就是它不加载图像的原因。在您正在执行的相同代码中,尝试提供 imageView 的背景颜色并进行检查。

于 2013-04-04T07:46:37.223 回答