0

我想返回一组缓存图像。查看代码。我只是想返回我创建的数组,但我不能,dispatch_get_main_queue而且我不知道如何更改代码以返回数组。我确信这很容易,但我在较低级别的 C / GCD 语法上遇到了困难。谢谢。

-(NSArray *)returnArray
{

    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"something" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:plistPath];

    NSMutableArray *finalArray = [[NSMutableArray alloc] initWithCapacity:array.count];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);


    dispatch_async(queue, ^{
       ]
        for(int i=0;i<array.count; i++) {

            NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[array objectAtIndex:i]];

            UIImage *frameImage = [UIImage imageWithContentsOfFile: filePath];
            UIGraphicsBeginImageContext(frameImage.size);
            CGRect rect = CGRectMake(0, 0, frameImage.size.width, frameImage.size.height);
            [frameImage drawInRect:rect];
            UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            [finalArray addObject:renderedImage];

        }

        dispatch_sync(dispatch_get_main_queue(), ^{
            //THIS IS WHAT IM TRYING TO DO, BUT I CANT
            return finalArray;
        });

    });

}
4

1 回答 1

1

你不能在不牺牲异步的情况下做到这一点。您需要创建一个将该值作为参数的函数,并将 finalArray 作为参数传递给它。因此,不是您的控制流看起来像这样(-> 表示“调用”,<- 表示“返回”):

returnArray -> 
    async (private queue) -> 
        sync (main queue) <-
    ... <-
returnArray <-

它看起来像这样:

returnArray -> 
    async (private queue) -> 
        sync (main queue) ->
            useArray (array)
于 2013-02-05T20:53:02.913 回答