Kevin Pimentel
Kevin Pimentel

Reputation: 1916

Image Gallery Flashing a black screen before UIScrollview is visible

I am fetching an array of images that I have stored in PARSE and it all seems to be working fine, except that I am getting a blinking black flashing screen before the UIScrollView is visible. I believe that the black flash is this line:

[carousel setBackgroundColor:[UIColor blackColor]];

The background of the UIScrollView seems to be shown every time that I am loading a new image onto the carousel. I suspect that this is the cause of the blinking black screen.

NOTE This code is inside of a PFTableViewCell

Here is my code:

[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];
   } 
}];

My question is what is causing this blinking black screen? And how can I fix it?

Thanks!

Upvotes: 1

Views: 219

Answers (1)

Kevin Pimentel
Kevin Pimentel

Reputation: 1916

I ended up moving this piece of code out of my the method which was being called often:

    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];

I moved that into the init and that did the trick for me. Now I just initialize the UIScrollView once and remove/add subviews when I need them refreshed.

Upvotes: 1

Related Questions