开发者社区 问答 正文

返回设置在GCD块中的值

有一个从block中获取的值,我希望返回到下面的方法中,这个block好像是异步的,不知道怎么样实现?

-(UIImage*) imageAtIndex:(NSUInteger)index
{
    UIImage *image;
    [self.album enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:index] options:0 usingBlock: ^(ALAsset *result, NSUInteger index, BOOL *stop)
     {
            //set image in here
     }];

    return image;
}

展开
收起
爵霸 2016-03-26 09:49:16 1853 分享 版权
1 条回答
写回答
取消 提交回答
  • -(void) imageAtIndex:(NSUInteger)index //since block is async you may not be able to return the image from this method
    {
    
        UIImage *image;
        [self.album enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:index] options:0 usingBlock: ^(ALAsset *result, NSUInteger index, BOOL *stop)
         {
             //set image in here
              dispatch_async(dispatch_get_main_queue(), ^{ //if UI operations are done using this image, it is better to be in the main queue, or else you may not need the main queue.
                 [self passImage:image]; //do the rest of the things in `passImage:` method
              });
         }];
    }

    这是我的实现方法,你试试吧

    2019-07-17 19:15:24
    赞同 展开评论
问答地址: