我遇到了一个我花了很长时间研究的奇怪问题。
我基本上是在尝试从资产库中获取照片的文件路径,并使用以下代码使用 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 实际上是在块之前解析的 - 因此它是 Null,因为 self.pdfImage 仅在块内设置。
如果我将这些代码行放在块中,我会收到 CGRectMake 方法的错误,即 Invalid Context 0x0。
我到底如何才能先分配 self.pdfImage UIImage 然后绘制它?