Objective c 为什么不';保留在资产库块外部的t变量?

Objective c 为什么不';保留在资产库块外部的t变量?,objective-c,ios,core-data,objective-c-blocks,alassetslibrary,Objective C,Ios,Core Data,Objective C Blocks,Alassetslibrary,我遇到了一个我花了很多时间研究的奇怪问题 我基本上是尝试从资产库中获取照片的文件路径,并使用CGRectMake方法在pdf上绘制它,代码如下: 在.h文件中: @property (strong, nonatomic) UIImage *pdfSnagImage; 在.m文件中: NSURL *url = [[NSURL alloc] initWithString:pdf.photo]; ALAssetsLibrary* library = [[ALAssetsLibrary alloc]

我遇到了一个我花了很多时间研究的奇怪问题

我基本上是尝试从资产库中获取照片的文件路径,并使用CGRectMake方法在pdf上绘制它,代码如下:

在.h文件中:

@property (strong, nonatomic) UIImage *pdfSnagImage;
在.m文件中:

NSURL *url = [[NSURL alloc] initWithString:pdf.photo];
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

[library assetForURL:url resultBlock:^(ALAsset *asset) {

    ALAssetRepresentation *rep = [asset defaultRepresentation];            
    self.filename = [rep filename];
    NSLog(@"filename for image is: %@", self.filename);

    CGImageRef iref = [rep fullResolutionImage];
    if (iref) {
        self.pdfImage = [UIImage imageWithCGImage:iref];
        NSLog(@"image height %f", self.pdfImage.size.height);

    }

} failureBlock:^(NSError *error) {

    NSLog(@"Couldn't load asset %@ => %@", error, [error localizedDescription]);

}];

UIImage *testImage = self.pdfImage;

[testImage drawInRect:CGRectMake( (pageSize.width - testImage.size.width/2)/2, 350, testImage.size.width/2, testImage.size.height/2)];
发生的情况是,块之后的UIImage、testImage实际上是在块之前解析的-因此它为空,因为self.pdfImage仅在块内设置

如果我将这些代码行放在块中,那么CGRectMake方法会出错,上下文无效0x0


究竟如何才能先分配self.pdfImage UIImage,然后再绘制它?

该块是异步执行的……它是在一个单独的线程上执行的。这是资产库api的设计

你的背景是什么?在调用
resultBlock
failureBlock
中的代码之前,方法
assetForURL:resultBlock:failureBlock:
立即返回,然后异步运行。因此,你需要在这两个模块中的任何一个模块中完成你的工作。下面的问题似乎是相关的:@Paul.s你能解释一下你所说的我所处的环境是什么意思吗?