【问题标题】:I have some remote images I'm loading into an NSMutableArray but the array isn't populated by the time I need it我有一些远程图像要加载到 NSMutableArray 中,但是在我需要它的时候没有填充该数组
【发布时间】:2014-07-31 13:41:15
【问题描述】:

在滑动手势和我自己的图片库滑块遇到麻烦之后,我决定使用预制的。

我正在使用 iCarousel,它运行良好。我已经使用一个填充了非远程图像的数组进行了测试。

在使用编码器的 init 中我这样做了:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        _additionalGarmentImagesArray = [[NSMutableArray alloc] init];
        UIImage *image1 = [UIImage imageNamed:@"asos.png"];
        UIImage *image2 = [UIImage imageNamed:@"neck.png"];
        UIImage *image3 = [UIImage imageNamed:@"diamind.png"];

        [_additionalGarmentImagesArray addObject:image1];
        [_additionalGarmentImagesArray addObject:image2];
        [_additionalGarmentImagesArray addObject:image3];
    }

    return self;
}

这很好用。但是它对我没有用,因为我的图像是远程图像。所以我使用了我自己的图片库中使用的代码,但图片没有按时下载。

在我看来DidLoad:

   PFQuery *query = [PFQuery queryWithClassName:@"Garments"];
    [query whereKey:@"title" equalTo:[self garmentTitle]];
    [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {

        if (!error) {

            PFFile *additionalImage1 = [object objectForKey:@"image2"];
            PFFile *additionalImage2 = [object objectForKey:@"image3"];
            PFFile *additionalImage3 = [object objectForKey:@"image4"];
            PFFile *additionalImage4 = [object objectForKey:@"image5"];
            PFFile *additionalImage5 = [object objectForKey:@"image6"];

            // Add main image to array first, the one passed over from previous controller
            [_additionalGarmentImagesArray addObject:[self garmentImage]];

            if (additionalImage1) {

                PFImageView *pfImageView1 = [[PFImageView alloc] init];
                [pfImageView1 setFile:additionalImage1];
                [pfImageView1 loadInBackground:^(UIImage *image, NSError *error) {
                    if (!error) {
                        [_additionalGarmentImagesArray addObject:image];
                    }

                }];
            }

            if (additionalImage2) {

                PFImageView *pfImageView2 = [[PFImageView alloc] init];
                [pfImageView2 setFile:additionalImage2];
                [pfImageView2 loadInBackground:^(UIImage *image, NSError *error) {
                    if (!error) {
                        [_additionalGarmentImagesArray addObject:image];
                    }

                }];
            }

            if (additionalImage3) {

                PFImageView *pfImageView3 = [[PFImageView alloc] init];
                [pfImageView3 setFile:additionalImage3];
                [pfImageView3 loadInBackground:^(UIImage *image, NSError *error) {
                    if (!error) {
                        [_additionalGarmentImagesArray addObject:image];
                    }

                }];
            }

        } else {
            NSLog(@"empty array");
        }            
    }];

这就是需要图像数组的地方:

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [_additionalGarmentImagesArray count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{

    PFImageView *imageView = [[PFImageView alloc] initWithFrame:CGRectMake(0, 0, 300.0f, 380)];
    view = imageView;

    //set image
    ((PFImageView *)view).image = _additionalGarmentImagesArray[index];

    return view;
}

viewDidAppear 方法显示数组已填充:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
     NSLog(@"Images Count: %i", [_additionalGarmentImagesArray count]);
}

请帮我解决这个问题。我的图像是远程存储的,我需要一种方法让它们在控制器加载时准备好,以便 iCarousel 可以使用它们,或者让 iCarousel 等待,然后在填充数组时自行刷新。

甚至可能在前一个控制器 UICollectionView 中做一些事情。我可以点击单元格,然后从单元格对象中获取我需要的图像,然后填充一个数组。一旦图像在数组中,唯一允许推送/segue发生。然而我不喜欢那样。这意味着用户在点击后必须等待才能转到 iCarousel 所在的详细视图控制器。

如果可能的话,将不胜感激一些解决方案。几个小时过去了,我几乎没有什么进展。

感谢您的宝贵时间

【问题讨论】:

  • 不知道这是否有帮助,但 UIImageView+AFNetworking.h 类别非常有用。不知道集成到 iCarousel 中会有多容易。
  • 目前使用 parse.com 的 PFImageView 做同样的事情。似乎不够快,或者我只是没有正确集成到 iCarousel。
  • 为什么每次都创建 PFImageView ,它应该被重用。只需检查 (view==nil) 是否足够。
  • 现在完成。没有不同。我的视图确实出现了方法,但显示数组已填充。

标签: objective-c cocoa-touch uiviewcontroller nsmutablearray icarousel


【解决方案1】:

解决方案是在我确实拥有数组的时候重新加载数据。因此,执行此操作的最佳位置是在 viewDidAppear 方法中。

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{

    PFImageView *imageView = [[PFImageView alloc] initWithFrame:CGRectMake(0, 0, 300.0f, 400)];
    view = imageView;

    //create new view if no view is available for recycling
    if (view == nil)
    {
       //set image
       ((PFImageView *)view).image = _additionalGarmentImagesArray[index];

    } else {
       //set image
       ((PFImageView *)view).image = _additionalGarmentImagesArray[index];
    }

    return view;
}

重新加载数据:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
     NSLog(@"Images Count: %i", [_additionalGarmentImagesArray count]);
    [[self carousel] reloadData];

}

【讨论】:

  • 我仍然不喜欢这里的想法。我宁愿在加载此控制器之前将图像全部加载到其他地方。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 2010-10-15
  • 2011-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多