使用Objective-C以pdf绘制照片

时间:2012-04-13 11:15:56

标签: objective-c ios pdf drawrect alassetslibrary

我很难搞清楚这一点,并且非常感谢一些帮助。

基本上,我正在尝试在用户按下按钮时生成的pdf文档中绘制存储在资源库中的照片。

我可以使用以下代码在pdf中绘制应用程序资源文件夹中包含的.png图像:

- (void) drawImage
{
    UIImage * demoImage = [UIImage imageNamed:@"demo.png"];
    [demoImage drawInRect:CGRectMake( (pageSize.width - demoImage.size.width/2)/2, 350, demoImage.size.width/2, demoImage.size.height/2)];
}

但是,我需要首先使用块从资产库中取出照片。例如:

NSURL *url = [[NSURL alloc] initWithString:image.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);

            }

            [self.pdfImage drawInRect:CGRectMake( (pageSize.width - self.pdfImage.size.width/2)/2, 350, self.pdfImage.size.width/2, self.pdfImage.size.height/2)];

        } 
            failureBlock:^(NSError *error) 
        {

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

        }];

    }

文件名作为.jpg文件成功打印到日志。 self.pdfImage.size.height也会打印到日志,告诉我它正在查找图像。

但是,调用drawInRect方法时图像为空并抛出错误:Invalid Context: 0x0.

0 个答案:

没有答案