Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a video on a UICollectionViewCell with a background thread to create seamless scrolling

I am working with an Objective-C application that features a scene with a full screen horizontally scrolling UICollectionView. I currently have a function being called every time a new cell appears on screen during scrolling that takes about 3-4 seconds to run and edits ui elements within the newly appeared cell. Because of this, my app lags every time a new cell enters the screen for about 4 seconds and then continues to scroll normally.

Is there a way to edit this function and put it on a background thread so that instead of a a 4 second lag in scrolling, there is seamless, uninterrupted scrolling? I understand that this solution will cause a 4 second delay to the ui elements from appearing but I am fine with that as long as there is a seamless scroll.

EDIT: I didn't think it would be a good idea to post code for this problem since the code in function that does the loading requires a video player api that I purchased, but I will go ahead and post the method below to show what parts of the function edit the UI.

//variables declared in other parts of the file
KolorEyes *myKolorEyesPlayer;
UICollectionView *collectionViewFollwersFeed;
NSIndexPath *lastPath = nil;

//called repeatedly while scrolling
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{


    if(scrollView==_collectionViewFollwersFeed)
    {

        [self tsLoad]; //function call

    }
}


- (void)tsLoad {

    //calculate index path of cell in the center of the screen
    NSIndexPath *centerCellIndexPath =
    [self.collectionViewFollwersFeed indexPathForItemAtPoint:
     [self.view convertPoint:[self.view center] toView:self.collectionViewFollwersFeed]];

    if(centerCellIndexPath != lastPath) //condition is satisfied if a new cell has been scrolled to the center
    {   
        lastPath = centerCellIndexPath;

        UICollectionViewCell *cell; 
        NSString *CellIdentifier;

        //initialize cell from center path
        CellIdentifier = @"FollowersFeed";
        cell = [_collectionViewFollwersFeed cellForItemAtIndexPath:centerCellIndexPath]; 

        //get respective url for video player
        NSMutableDictionary *dict1=[follwerFeed objectAtIndex:centerCellIndexPath.row];
        NSString *tsstring= [dict1 valueForKey:@"video"];
        NSURL *tsurl = [[NSURL alloc] initWithString:tsstring]; 

        //view that the video will be played in
        UIView *tsView = (UIView *)[cell viewWithTag:99]; 

        //API-specific parameters for video player session
        KolorEyesSessionParams *params = [[KolorEyesSessionParams alloc] init];
        params.delegate = self;
        /* other params properties set like above at this point...
           ...
           ...
        */

        //API-specific initialization
        myKolorEyesPlayer = [[KolorEyes alloc] initWithParams:params];

        //API-specific parameters for video player view
        KolorEyesRenderViewParams *fparams = [[KolorEyesRenderViewParams alloc] init];
        fparams.cameraFieldOfView = 90;
        /* other fparams properties set like above at this point...
           ...
           ...
        */

        //API-specific initializations
        id _tsViewHandle = [myKolorEyesPlayer setRenderView: tsView withParams:fparams];
        [myKolorEyesPlayer setCameraControlMode:kKE_CAMERA_MOTION forView:_tsViewHandle];

        __block KolorEyes *player = myKolorEyesPlayer;

        //error checking
        eKEError err = [myKolorEyesPlayer setAssetURL: tsurl
                                withCompletionHandler:^{
                                    // Media is loaded, play now
                                    [player play];
                                }];

        if (err != kKE_ERR_NONE) {
            NSLog(@"Kolor Eyes setAssetURL failed with error");
        }
    } 
}
like image 858
Roger99 Avatar asked Jan 31 '17 23:01

Roger99


1 Answers

You can define an “NSOperationQueue“ for running background operations and when cell is visible you can load video to cell of UICollectionView.

Also, don’t forget to take advantage of the NSOperationQueue and call “cancelAllOperations” when the collectionView is not needed anymore

like image 172
Zalak Patel Avatar answered Sep 22 '22 17:09

Zalak Patel