IOS 照片选取 同步处理

照片选取完成过程是异步进行,使用信号量阻塞这个过程。


 ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
//    @weakify(assetsLibrary);
// 创建 一个信号量
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
for (NSURL *assetUrl in selectedAssetURLs) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        //NCLog(@"%@",@"处理 多张照片");
        [assetsLibrary assetForURL:assetUrl resultBlock:^(ALAsset *asset) {
            if (asset) {
                UserAlbum *albumImg = [[UserAlbum alloc] init];
                albumImg.uploadState = UserAlbumImageUploadStateInit;
                UIImage *highQualityImage = [UIImage fullScreenImageALAsset:asset];
                UIImage *thumbnailImage = [UIImage imageWithCGImage:[asset thumbnail]];
                albumImg.image = highQualityImage;
                albumImg.thumbnailImage = thumbnailImage;
                [self.images addObject:albumImg];
            }
            //信号量 通知 +1
            dispatch_semaphore_signal(sema);
        } failureBlock:^(NSError *error) {
            dispatch_semaphore_signal(sema);
        }];
    });
    //dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
//阻塞进程 直到 图片处理完毕
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    原文作者:hqman
    原文地址: https://segmentfault.com/a/1190000002923635
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞