ios - 在 iOS 中如何在从 Assets 中获取图像和视频时应用延迟加载?

标签 ios

我已经从图库中获取了图像和视频,当视频和图像的数量最少时它工作正常,但是当图像和视频的数量很大时,它会阻塞主线程。

-(void)viewDidAppear:(BOOL)animated{

[NSThread sleepForTimeInterval:1];
// call the same method on a background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    [self getAllVideosAndImages];

});
}

从 Assets 中获取图像和视频

-(void)getAllVideosAndImages{
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
assetGroups = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
NSUInteger groupTypes = ALAssetsGroupAll;

void (^assetEnumerator)
( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
    if(result != nil) {
        if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]||[[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
            ALAssetRepresentation *rep = [result defaultRepresentation];

            CGImageRef iref = [rep fullResolutionImage];
            if (iref) {

                //GET DATE
                NSDate *myDate = [result valueForProperty:ALAssetPropertyDate];
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"MMM dd,YYYY"];
                NSString *realDateStr = [dateFormatter stringFromDate:myDate];
                //GET IMG AND VIDEO
                UIImage *largeimage = [UIImage imageWithCGImage:iref];
                NSData *webData = UIImagePNGRepresentation(largeimage);
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0];
                NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"img%zd.mov",imgName]];
                imgName=imgName+1;
                [webData writeToFile:localFilePath atomically:YES];
                Para_ImageSet *objImgSet=[Para_ImageSet new];
                objImgSet.imagePath=localFilePath;
                objImgSet.imageDate=realDateStr;
               // [yArrayOfAllImges addObject:objImgSet];

                if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
                    objImgSet.is_video=YES;
                }
                else
                {
                     objImgSet.is_video=NO;
                }

                if ([tempDate isEqualToString:@""]) {
                    yArrayOfImagesWithDate=[NSMutableArray new];
                    [yArrayOfImagesWithDate addObject:objImgSet];
                    tempDate=realDateStr;
                }else
                {

                        //GATE IMAGE COUNT
                        if ([realDateStr isEqualToString:tempDate]) {
                            //DO NOTHING
                           [yArrayOfImagesWithDate addObject:objImgSet];

                        }
                        else
                        {



                            [yArrayOFNNestedImgs addObject:yArrayOfImagesWithDate];
                             NSLog(@" ----------ImagesWithDate =%zd, %@",yArrayOfImagesWithDate.count,tempDate);
                            yArrayOfImagesWithDate=[NSMutableArray new];
                            [yArrayOfImagesWithDate addObject:objImgSet];
                            tempDate=realDateStr;







                        }
                }


            }

        }

    }

    [self.tableView reloadData];
};

void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
    if(group != nil) {
        [group enumerateAssetsUsingBlock:assetEnumerator];
        [assetGroups addObject:group];
      //  NSNumber *countObj = [NSNumber numberWithInt:count];
        NSLog(@" ----------ImagesWithDate =%zd, %@",yArrayOfImagesWithDate.count,tempDate);
        [yArrayOFNNestedImgs addObject:yArrayOfImagesWithDate];
        NSLog(@" ----------Count =%zd",yArrayOFNNestedImgs.count);

        [NSThread sleepForTimeInterval:10];
        // update UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });

    }
};


[library enumerateGroupsWithTypes:groupTypes
                       usingBlock:assetGroupEnumerator
                     failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}];
}

请建议..提前致谢

最佳答案

[NSThread sleepForTimeInterval:1]; 正在阻塞主线程一秒钟。不需要。

你可以使用[self performSelectorInBackground:@selector(getAllVideosAndImages) withObject:nil];

还有,先把[self.tableView reloadData];包起来 dispatch_async(dispatch_get_main_queue(), ^{ ... });也是

关于ios - 在 iOS 中如何在从 Assets 中获取图像和视频时应用延迟加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32400646/

相关文章:

ios - 我删除了包含 UIView Controller 类 * 的类文件。和 *.h 文件

ios - 如何在 iOS 上检测每个应用程序的数据

ios - 调用 NSLayoutConstraint.activateConstraints 会使约束无效吗?

iphone - UIButton 位于 UILabel 文本+iPhone 之间

iphone - 用于 UIImageView/UIView 时如何调整使用 resizableImageWithCapInsets 创建的 UIImage 的大小

ios - AVCaptureDevice 在模拟器上始终为 null

javascript - CSS 样式仅适用于 iOS 网络应用程序?

ios - 无法更改 uitableView 中的按钮图像

ios - 水平滚动时 ScrollView 的默认方法是什么?

iphone - TableView - 信号 SIGABRT