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