麻烦从异步块向主线程发送参数

时间:2013-01-03 05:56:57

标签: iphone ios cocoa-touch grand-central-dispatch alassetslibrary

我有以下方法在拍摄照片后从ALAsset检索UIImagePicker。然后它尝试通过主线程将此ALAsset发送到我的另一个方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSURL *imageURL = [[info valueForKey:UIImagePickerControllerReferenceURL] retain];
    __block ALAsset *result;

    [self.assetsLibrary assetForURL:imageURL resultBlock:^(ALAsset *asset)
    {
        result = [asset retain];

        dispatch_async(dispatch_get_main_queue(), ^
        {
            [self loadPhotoImageViewWithAsset:result];
            [self dismissModalViewControllerAnimated:YES];
            [imageURL release];
            [result release];
        });
    }
    failureBlock:^(NSError *error)
    {

    }];
}

当我进入dispatch_async(dispatch_get_main_queue(), ^ block时,result显示为nil。谁知道我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

请参阅UIImagePickerController to use for a UIImageView,结果显示为nil。

像这样使用:

 [self.assetsLibrary assetForURL:imageURL resultBlock:^(ALAsset *asset)
{
    result = [asset retain];
    [self loadPhotoImageViewWithAsset:result];
    [self dismissModalViewControllerAnimated:YES];
    [imageURL release];
    [result release];   
}
failureBlock:^(NSError *error)
{

}];

更改loadPhotoImageViewWithAsset方法

-(void)loadPhotoImageViewWithAsset:(ALAsset *)asset
{
  //dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
   //dispatch_async(queue, ^{
    dispatch_async(dispatch_get_main_queue(), ^
    {
     //here code for loading image
    });
  // });
}