简体   繁体   English

无法使用SDWebImage显示图像

[英]unable to display image using SDWebImage

Newbie to iPhone here. iPhone新手在这里。 I have an image to display fetched from a rss feed. 我有一张要显示从rss feed获取的图像。 I'm currently using the code below which works but slows down loading elements in the view: 我目前正在使用下面的代码,但是可以减慢视图中元素的加载速度:

  for(int i=0; i<[bannerArray count]; i++){

       NSString *bannerImagestr = [[bannerArray objectAtIndex:i] BannerImage];
       bannerImagestr  = [ bannerImagestr  stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];

      NSURL *banURL= [NSURL URLWithString:bannerImagestr];



       NSData *data = [NSData dataWithContentsOfURL:banURL];

       imgEventDetail = [[[UIImage alloc] initWithData:data] autorelease];


       [banEventDetailArray addObject:imgEventDetail];

 }NSLog(@"the  banEventDetailArray is %@",banEventDetailArray);

I tried the SDWebImage api to make it load quick but i'm failing to get the image. 我尝试使用SDWebImage API使其快速加载,但无法获取图像。 The code which i've been using is below: 我一直在使用的代码如下:

   for(int i=0; i<[bannerArray count]; i++){

       NSString *bannerImagestr = [[bannerArray objectAtIndex:i] BannerImage];
       bannerImagestr  = [ bannerImagestr  stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];

      NSURL *banURL= [NSURL URLWithString:bannerImagestr];


       [banIconImage setImageWithURL:banURL placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

      banImg=banIconImage.image;//<<<-------- updated here banImg is an instance of UIImage

       [banEventDetailArray addObject:banImg];

 }NSLog(@"the  banEventDetailArray is %@",banEventDetailArray);

I need the banEventDetailArray in the form of UIImage , because I'm setting this array in the below code which takes (UIImage *) as its parameter 我需要UIImage形式的banEventDetailArray ,因为我在下面的代码中设置了此数组,该代码以(UIImage *)作为其参数

 [eventsdetailroundedButtonType setBackgroundImage:[banEventDetailArray objectAtIndex:numTimerTicks] forState:UIControlStateNormal];

Please help me find what I've been missing out and where I may have gone wrong. 请帮助我找到我遗漏的地方以及可能出错的地方。

Thanks in advance. 提前致谢。

UPDATE: i've replaced a line in the second code block. 更新:我已经替换了第二个代码块中的一行。 ie banImg=banIconImage.image; 即banImg = banIconImage.image; at line number 11. 在第11行。

You can use your image URL as key to find the cached image: 您可以将图片网址用作查找缓存图片的键:

UIImage *myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:[NSString \
stringWithFormat:@"%@",banURL]];

I guess the problem is in this lines: 我猜问题出在这行:

   [banIconImage setImageWithURL:banURL placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

   banImg=[[UIImage alloc]init];         // <-- here: empty image
   [banIconImage setImage:banImg];       // <-- here: overriding banURL image

since you are: 由于您是:

  1. first, getting the image from the URL; 首先,从网址获取图片;

  2. overriding that image with an empty one. 用一个空的图像覆盖该图像。

Use this code, instead: 使用此代码,而不是:

   [banIconImage setImageWithURL:banURL placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
   [banEventDetailArray addObject:banIconImage];

ie, you add the image your retrieve to the array and then use it later. 即,您将检索到的图像添加到数组中,然后在以后使用。

You need to keep track of the UIImageView because it is where the SDWebImage magics happens. 您需要跟踪UIImageView因为它是SDWebImage Indeed, that view handles the displays of the placeholder while the image is fetched; 确实,在获取图像时,该视图处理了占位符的显示。 when the image is there, it is replaced. 当图像在那里时,它将被替换。

You can get the image property only after the actual image is fetched, otherwise you will only get the placeholder. 仅在获取实际图像后才能获取image属性,否则只能获取占位符。

Why are you setting the image twice? 为什么要两次设置图像? Doesn't make sense. 没道理

 [banIconImage setImageWithURL:banURL placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

              banImg=[[UIImage alloc]init];
   [banIconImage setImage:banImg];

Try removing the second setting, should just be. 尝试删除第二个设置,应该是。

     [banIconImage setImageWithURL:banURL placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

EDIT: Logic still doesn't make sense. 编辑:逻辑仍然没有意义。 You're probably trying add the UIImageView 's image as an object in the array, which means your code should probably look like this. 您可能正在尝试将UIImageView的图像添加为数组中的对象,这意味着您的代码应该看起来像这样。

NSString *bannerImagestr = [[bannerArray objectAtIndex:i] BannerImage];
bannerImagestr  = [ bannerImagestr  stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSURL *banURL= [NSURL URLWithString:bannerImagestr];
[banIconImage setImageWithURL:banURL placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

[banEventDetailArray addObject:banIconImage.image];

Try the code before you decide it's not what you need, apparently you don't exactly know what you want. 在确定您不需要的代码之前先尝试一下代码,显然您并不完全知道自己想要什么。 If this is the answer to your question, mark it as the solution. 如果这是您的问题的答案,请将其标记为解决方案。

Your array does not hold image object. 您的数组不包含图像对象。 Instead it hold pointers (references) to UIImage objects. 而是保留指向UIImage对象的指针(引用)。 If you look at your working code, there you have: 如果您查看自己的工作代码,就会发现:

imgEventDetail = [[[UIImage alloc] initWithData:data] autorelease];

[banEventDetailArray addObject:imgEventDetail];

You create and add new instance of imgEventDetail UIImage object with data and then add it to the array. 您使用数据创建并添加imgEventDetail UIImage对象的新实例,然后将其添加到数组中。 You would need to do the same for your bottom (not working) code. 您需要对底部(不起作用)的代码执行相同的操作。

Try this in the not working code: 在不起作用的代码中尝试以下操作:

 [banEventDetailArray addObject:banIconImage.image];  // array automatically retains.

All assuming that your images are downloaded correctly using SDWebImage API. 所有这些都假定您使用SDWebImage API正确下载了图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM