利用assetForURL反读相册图片,并等待 assetForURL的block 调用(GCD信号)

来源:互联网 发布:profile直播软件 编辑:程序博客网 时间:2024/05/17 02:23


代码实现如下:

NSMutableArray *imageArr = [[NSMutableArray alloc] init];        ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];                dispatch_semaphore_t sema = dispatch_semaphore_create(0);        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);                for (NSDictionary *dic in photoImages)        {            if ([[dic objectForKey:@"flag"] boolValue])            {                dispatch_async(queue, ^{                                        //[imageArr addObject:[dic objectForKey:@"fsImage"]];                                        NSURL *url= [dic objectForKey:@"url"];                    [assetLibrary assetForURL:url resultBlock:^(ALAsset *asset)  {                                                ALAssetRepresentation* representation = [asset defaultRepresentation];                        UIImage* fsImage = [UIImage imageWithCGImage:representation.fullScreenImage];                        [imageArr addObject:fsImage];                        dispatch_semaphore_signal(sema);                                            }failureBlock:^(NSError *error) {                        NSLog(@"error=%@",error);                        dispatch_semaphore_signal(sema);                    }                    ];                                    });                                dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);                            }        }                                [self.delegate getSelectImage:imageArr];        [self.navigationController popViewControllerAnimated:YES];


0 0
原创粉丝点击