使用后台线程在UICollectionViewCell上加载视频以创建无缝滚动

Rog*_*r99 6 multithreading objective-c ios uicollectionview uicollectionviewcell

我正在使用Objective-C应用程序,该应用程序具有全屏水平滚动UICollectionView的场景.我目前每次在滚动期间在屏幕上出现新单元格时都会调用一个函数,运行大约需要3-4秒,并在新出现的单元格中编辑ui元素.因此,每当新单元格进入屏幕大约4秒钟后,我的应用程序就会滞后,然后继续正常滚动.

有没有办法编辑这个功能,并把它放在一个后台线程,以便滚动时不是4秒延迟,有无缝,不间断的滚动?我知道这个解决方案会导致ui元素出现4秒的延迟,但只要有无缝滚动,我就可以了.

编辑:我认为发布此问题的代码不是一个好主意,因为执行加载的函数中的代码需要我购买的视频播放器API,但我会继续发布下面的方法来显示什么功能的一部分编辑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");
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

小智 0

有没有办法编辑这个函数并将其放在后台线程上,这样就可以实现无缝、不间断的滚动,而不是 4 秒的滚动延迟?

正如您提到的,您正在运行 SDK:

Kolor Eyes 是一款免费的 360 度视频播放器应用程序,可使用像素精确的投影算法渲染球形视频。用户可以随时通过触摸手势或设备运动控制来选择不同的相机视角。它不仅仅是一个播放器,还是专为 Kolor 360 视频托管网站设计的完美浏览器。本文档涉及 Kolor Eyes iOS 2.0 版

在 UICollectionView/UITableView 中运行 Apple 自己的“AVPlayer”而没有任何延迟的问题已经够多了,而且您想要运行定制的 360 度视频播放器而没有任何延迟。

由于它是一个 SDK,您无法访问或/和修改任何代码,您需要联系 SDK 的开发人员以获取指南。我不认为这个 SDK 是为了在滚动视图中运行而开发的,因为这似乎是一项繁重的任务(但我不确定 100%,你需要询问开发人员)。

但是,您不应该按照您的要求修改后台线程上的 UI,这不会帮助消除滞后,它会使情况变得更糟,甚至在大多数情况下崩溃或挂起您的应用程序,我确信(至少希望如此)开发人员已经使用此 SDK 进行了尽可能多的线程处理:

在Cocoa应用程序中,主线程运行用户界面,即所有绘图和所有事件都在主线程上处理。如果您的应用程序在该线程上执行任何冗长的同步操作,您的用户界面可能会变得无响应并触发旋转光标。为了避免这种情况,您应该缩短这些操作所消耗的时间,推迟它们的执行,或者将它们移动到辅助线程。

来源