1

我已经设法使用以下代码获取存储在资产库中的照片的原始文件名:

NSURL *url = [[NSURL alloc] initWithString:project.photo];
        ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

        [library assetForURL:url resultBlock:^(ALAsset *asset) {

            ALAssetRepresentation *rep = [asset defaultRepresentation];

            _filename = [rep filename];
            NSLog(@"filename for image is: %@", _filename);

            if (_filename != nil)
            {

                UIImage *image = [UIImage imageWithContentsOfFile:_filename];
                NSLog(@"image is! %f", image.size.height);

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

            }

NSString 变量 _filepath 将图像的原始文件路径存储为:

IMG_0294.JPG

我想做的就是从照片库中获取这张图片,并使用以下代码将其绘制在 pdf 文档中:

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

这个绘制代码肯定是有效的,因为我已经使用存储在应用程序目录中的图像进行了尝试。我只需要找到一种使用文件路径来创建 UIImage 或将其指向该照片的完整目录的方法。

输出到控制台时,图像变量返回 null。

任何帮助都会很棒。

4

2 回答 2

2

我建议你抓住url照片,而不仅仅是filename

imageURL = [rep url];

您可以将其imageURL与创建 UIImage的ALAssetsLibrary方法 一起使用。assetForURL:resultBlock:failureBlock:

有很多示例代码展示了如何使用该assetForURL方法。

希望这可以帮助。

于 2012-04-02T21:23:31.757 回答
0

您无法通过路径或 url 从资产库中获取图像。在您的情况下,您需要使用 UIImage *image = [UIImage imageWithCGImage:rep.fullResolutionImage];

或 fullScreenImage 或其他...

于 2012-04-10T20:40:03.273 回答