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然后绘制它?
你正在绘制什么样的上下文?方法'assetForURL:resultBlock:failureBlock:'立即返回,然后在调用'resultBlock'或'failureBlock'中的代码之前异步工作。因此,您需要在这些区块中进行您的工作。 – 2012-04-03 00:09:40
以下SO问题似乎相关: http://stackoverflow.com/questions/7234445/wait-for-assetforurl-blocks-to-be-completed – lukasz 2012-04-03 02:54:45
@ Paul.s你能解释一下你的是什么意思上下文我正在绘制? – jcrowson 2012-04-03 08:18:18