DaffodilGirl 2013-01-11 01:47 采纳率: 0%
浏览 2047

返回设置在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_ 2013-01-11 06: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
              });
         }];
    }
    

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

    评论

报告相同问题?

悬赏问题

  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败