0

何年もかけて取り組んできた奇妙な問題に出くわしました。

私は基本的に、アセットライブラリから写真のファイルパスを取得し、以下のコードを使用して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 が実際にはブロックの前に解決されているということです。

これらのコード行をブロックに入れると、CGRectMake メソッドで Invalid Context 0x0 というエラーが発生します。

self.pdfImage UIImage を最初に割り当ててから描画するにはどうすればよいですか?

4

1 に答える 1

1

ブロックは非同期で実行されます...別のスレッドで実行されています。これは、assets lib api の設計です。

于 2012-04-10T20:36:25.740 に答える