图像库在UIScrollview可见之前闪烁黑屏

时间:2014-10-05 00:40:14

标签: ios objective-c uitableview parsing uiscrollview

我正在获取一个存储在PARSE中的图像数组,这一切看起来都很好,除了在UIScrollView可见之前我得到一个闪烁的黑色闪烁屏幕。我相信黑色闪光是这一行:

[carousel setBackgroundColor:[UIColor blackColor]];

每次我将新图像加载到轮播上时,都会显示UIScrollView的背景。我怀疑这是黑屏闪烁的原因。

注意此代码位于PFTableViewCell

这是我的代码:

[PFObject fetchAllIfNeededInBackground:[gallery objectForKey:kFTPostPostsKey] block:^(NSArray *objects, NSError *error) {
    if (!error) {

        carousel = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320.0f,320.0f)];
        [carousel setUserInteractionEnabled:YES];
        [carousel setDelaysContentTouches:YES];
        [carousel setExclusiveTouch:YES];
        [carousel setCanCancelContentTouches:YES];
        [carousel setBackgroundColor:[UIColor blackColor]];

        //add the scrollview to the view
        carousel.pagingEnabled = YES;
        [carousel setAlwaysBounceVertical:NO];

        int i = 0;
        for (PFObject *post in objects) {
            PFFile *file = [post objectForKey:kFTPostImageKey];
            [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                if (!error) {

                    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapImageInGalleryAction:)];
                    singleTap.numberOfTapsRequired = 1;

                    CGFloat xOrigin = i * 320;
                    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, 320.0f, 320.0f)];
                    [imageView setBackgroundColor:[UIColor blackColor]];
                    UIImage *image = [UIImage imageWithData:data];
                    [imageView setImage:image];
                    [imageView setClipsToBounds:YES];
                    [imageView setContentMode:UIViewContentModeScaleAspectFill];
                    [imageView setUserInteractionEnabled:YES];
                    [imageView addGestureRecognizer:singleTap];
                    [carousel addSubview:imageView];
                }
            }];
            i++;

        }

        [carousel setContentSize: CGSizeMake(320.0f * objects.count, 320.0f)];
        [self.galleryButton addSubview:carousel];
        [self.imageView setHidden:NO];
   } 
}];

我的问题是什么导致这个闪烁的黑屏?我该如何解决?

谢谢!

1 个答案:

答案 0 :(得分:1)

我最终将这段代码移出了我经常调用的方法:

    carousel = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320.0f,320.0f)];
    [carousel setUserInteractionEnabled:YES];
    [carousel setDelaysContentTouches:YES];
    [carousel setExclusiveTouch:YES];
    [carousel setCanCancelContentTouches:YES];
    [carousel setBackgroundColor:[UIColor blackColor]];

    //add the scrollview to the view
    carousel.pagingEnabled = YES;
    [carousel setAlwaysBounceVertical:NO];

我把它移到了init,这对我有用。现在我只需初始化UIScrollView一次,并在需要刷新时删除/添加子视图。