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


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

本文固定链接: http://www.js-code.com/ios/ios_53306.html