无法加载与FSPagerView异步的图像

时间:2018-06-22 13:01:35

标签: ios objective-c xcode

我在滑块中显示图像时遇到问题,因为必须等待它们下载然后再将它们加载到FSPagerViewCell ImageView中。

在委托函数中

- (FSPagerViewCell *)pagerView:(FSPagerView *)pagerView cellForItemAtIndex:(NSInteger)index

我正在使用异步功能,当图像准备就绪时,它将返回下载的图像。 我用

dispatch_async(dispatch_get_main_queue(), ^{

将图像加载到FSPagerViewCell.imageView

但是它什么也没显示,只是空白。

这是完整的代码:

- (FSPagerViewCell *)pagerView:(FSPagerView *)pagerView cellForItemAtIndex:(NSInteger)index
{
    FSPagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"pagerCell" atIndex:index];

    if (index < [arrMediaGallery count])
    {
    pageControl.currentPage=index;

    MediaGallery *firstImage = arrMediaGallery[index];
    NSString *imageName = firstImage.Source;
    NSString *imageURL;

    if (imageName != nil && [imageName isEqualToString:@"NULL"] == NO && [imageName isEqualToString:@""] == NO) {

        imageName = [NSString stringWithFormat:@"thumblarge_%@", imageName];
        imageURL = [NSString stringWithFormat:@"%@/cache/photos/%@", [MyApp sharedInstance].settings.StaticURL, imageName];
    }

    UIImage *image = [NetworkUtilities resolveImageWithName:imageName];

    if (image == nil) {


        ProcessServerData *processData = [ProcessServerData new];
        [processData downloadImageForURL:imageURL verifyDownload:NO
                              verifyHash:nil
                            oldImageName:nil
                              parentView:[cell contentView]
                              completion:^(NSError *error) {

                                  UIImage* image = [NetworkUtilities resolveImageWithName:[NSString stringWithFormat:@"%@", imageName]];

                                  dispatch_async(dispatch_get_main_queue(), ^{

                                  [UIView transitionWithView:cell.imageView
                                                    duration:2.0f
                                                     options:UIViewAnimationOptionTransitionCrossDissolve
                                                  animations:^{
                                                      [cell.imageView setImage:image];
                                                  } completion:nil];
                                  });


                              }];

    }
    else {
        [cell.imageView setImage:image];
    }

    }
    return cell;
}

我做错什么了吗?

1 个答案:

答案 0 :(得分:0)

步骤:1-从“ https://github.com/WenchaoD/FSPagerView”安装

步骤:2-在VC中导入“ FSPagerViewDataSource,FSPagerViewDelegate”

步骤:3-实现方法

 //MARK: - FSPagerview Delegate and Datasource -
func numberOfItems(in pagerView: FSPagerView) -> Int
{
    if pagerView == self.News_images_View
    {
        return 3
    }
    else
    {
        return 3
    }
}

func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell
{
    let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
    cell.imageView?.contentMode = .scaleAspectFit
    cell.imageView?.clipsToBounds = true


    if pagerView == self.News_images_View
    {
        cell.imageView?.image = UIImage(named: "noticia.jpg")
    }
    return cell
}

func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int)
{
    if pagerView == self.News_images_View
    {

    }
}
  

@IBOutlet var viewFirstAdvView:FSPagerView!

  • 内部ViewDidLoad
  
    

self.viewFirstAdvView.register(FSPagerViewCell.self,forCellWithReuseIdentifier:“ cell”)self.viewFirstAdvView.itemSize =     CGSize(width:self.view.frame.size.width,height:                 UIScreen.main.bounds.size.width / 2)             self.viewFirstAdvView.isInfinite = true             self.viewFirstAdvView.automaticSlidingInterval = 3.0

  
     

self.lastAdvertiseView.transformer =   FSPagerViewTransformer(type:.crossFading)