2 daffodilgirl DaffodilGirl 于 2013.01.11 09:47 提问

返回设置在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;
}

1个回答

redCoral_
redCoral_   2013.01.11 14:16
-(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
          });
     }];
}

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!