UIImage没有出现在iPad设备上,但它出现在iPad模拟器上

时间:2012-04-18 07:34:36

标签: objective-c ios ipad ios5 uiimage

这是我正在使用的代码。请记住,这在模拟器中工作得很好。但是,当我在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 个答案:

答案 0 :(得分:4)

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

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

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

另一个编辑 - 代表!

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

尝试创建UIButton时,可能无法完成异步下载。最后一段代码应该在下载完成后调用并在if语句中调用(据我可以从这段代码中看到)。

答案 3 :(得分: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];
}

显然这是非常基本的 - 你需要添加东西来重置表,或者如果有多个文件将图像写入文件。希望它有所帮助!