0

我遇到了一個奇怪的問題,我花了很多年的工作。爲什麼變量不保存在Asset-Library塊之外?

基本上,我試圖讓照片的文件路徑出資產庫,並繪製它使用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.pdfIm年齡只在該區塊內設置。

如果我把這些代碼行放在代碼塊中,我得到一個CGRectMake方法的錯誤,無效上下文0x0。

我怎樣才能先指定self.pdfImage UIImage然後繪製它?

+0

你正在繪製什麼樣的上下文?方法'assetForURL:resultBlock:failureBlock:'立即返回,然後在調用'resultBlock'或'failureBlock'中的代碼之前異步工作。因此,您需要在這些區塊中進行您的工作。 – 2012-04-03 00:09:40

+0

以下SO問題似乎相關: http://stackoverflow.com/questions/7234445/wait-for-assetforurl-blocks-to-be-completed – lukasz 2012-04-03 02:54:45

+0

@ Paul.s你能解釋一下你的是什麼意思上下文我正在繪製? – jcrowson 2012-04-03 08:18:18

回答

1

該塊是異步執行的......它在一個單獨的線程上執行。這是資產lib api的設計。

相關問題