1

这是我正在使用的代码。请记住,这在模拟器中工作得很好。但是,当我在 iPad 设备上测试时运行相同的代码时,它不会加载图像。为什么会这样?

SDWebImageManager *manager = [SDWebImageManager sharedManager];
NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.net/folder/Photos/thumbnails/IMG_1780.JPG"];
UIImage *cachedImage = [manager imageWithURL:url];

if (cachedImage)
{
    // Use the cached image immediatly
}
else
{
    // Start an async download
    [manager downloadWithURL:url delegate:self];
}


UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(80+196, 5, 192, 192)];
[button setImage:cachedImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(imageSelected:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
return cell;

我将不胜感激任何帮助!谢谢!

编辑:修改后的代码,仍然可以在 Sim 中使用,但不能在设备上使用。

    - (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
{
    // Do something with the downloaded image
    //testImage = image;
    testButton = [[UIButton alloc] initWithFrame:CGRectMake(80+196, 5, 192, 192)];
    [testButton setImage:image forState:UIControlStateNormal];
    [testButton addTarget:self action:@selector(imageSelected:) forControlEvents:UIControlEventTouchUpInside];

}
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.net/album/Photos/thumbnails/IMG_1780.JPG"];
    UIImage *cachedImage = [manager imageWithURL:url];
    [self webImageManager:manager didFinishWithImage:cachedImage];
    if (cachedImage)
    {
        // Use the cached image immediatly
        //[self webImageManager:manager didFinishWithImage:cachedImage];
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(80+196, 5, 192, 192)];
        [button setImage:cachedImage forState:UIControlStateNormal];
        [button addTarget:self action:@selector(imageSelected:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:testButton];
    }
    else
    {
        // Start an async download
        [manager downloadWithURL:url delegate:self];
    }
4

4 回答 4

4

从未使用过 SDWebImageManager,但只是快速浏览了一下,看起来你做的一切都是正确的!另一方面,它不起作用,所以......

我曾经遇到过类似的问题,经过大量的头发撕裂后,我最终发现设备和模拟器上使用的文件系统是不同的——在你的 iPad 上,我相信它是区分大小写的,但在模拟器上不一样。仔细检查文件名的大小写,它可能会有所不同。

编辑-另外,我假设您正在尝试使用缓存的图像(这就是它立即出现的原因),但是如果您正在下载一个,那么我希望您的按钮初始化代码为时过早。

另一个编辑 - 代表!

你知道使用 SDWebImageManager 的委托吗?它可能有一个在图像下载时调用的方法 - 您需要在设置按钮的类中实现 SDWebImageManager 委托协议等 - 将有一个类似 -(void)imageDidFinishDownloading:(UIImage *)myImage 您必须包含它,这就是您需要将图像放入按钮的地方。

于 2012-04-18T07:45:31.790 回答
0

写一个新答案比编辑我的上一个答案更容易。

这是一些可能有用的代码 -

-(void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image {

    // unless you have a way to know which row it's going into, we'll just add it to the end of the array

    [downloadedImages addObject:image];

    [myTable reloadData];

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [downloadedImages count];
}


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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }


    NSInteger row = indexPath.row;

    UIImage *theImage = [downloadedImages objectAtIndex:row];

    UIButton *testButton = [[[UIButton alloc] initWithFrame:CGRectMake(80+196, 5, 192, 192)] autorelease];
    testButton.tag = row; // so we know which row the pressed button is in

    [testButton setImage:theImage forState:UIControlStateNormal];
    [testButton addTarget:self action:@selector(imageSelected:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:testButton];

    return cell;

}


-(void)imageSelected:(UIButton *)pressedButton {

    NSInteger row = pressedButton.tag;

    // now you know which row was pressed...
}

在此处更改您的代码(不要将其放在 tableView:cellForRowAtIndexPath: 方法中!) -

SDWebImageManager *manager = [SDWebImageManager sharedManager];
NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.net/folder/Photos/thumbnails/IMG_1780.JPG"];
UIImage *cachedImage = [manager imageWithURL:url];

if (cachedImage) {

    // Use the cached image immediately
    [downloadedImages addObject:cachedImage];

    [myTable reloadData];

} else {
    // Start an async download
    [manager downloadWithURL:url delegate:self];
}

显然这是非常基本的 - 你需要添加东西来重置表格,或者如果图像超过几个,则将图像写入文件。希望能帮助到你!

于 2012-04-18T12:53:58.873 回答
0

可能有点太明显了,但是,你检查过你的 iPad 是否有互联网连接吗?

于 2012-04-18T07:38:04.290 回答
0

当您尝试创建 UIButton 时,异步下载可能未完成。最后一段代码应该在下载完成后在if语句内部调用(据我从这段代码中可以看出)。

于 2012-04-18T07:38:13.597 回答