ios - CGImageSourceCreateImageAtIndex 返回 nil

标签 ios pdf uiimage cgimage

我正在尝试使用此解决方案将 pdf 文件转换为图像:

CFURLRef urlRef = (CFURLRef)[NSURL fileURLWithPath:@"http://uat.okitoo.fr/document/document/s595_506cb1811852f/o_specimen-page-seule.pdf"];
CGImageSourceRef src = CGImageSourceCreateWithURL(urlRef, NULL);
NSDictionary* options = [NSDictionary dictionaryWithObject:(id)[NSNumber numberWithInt:500] forKey:(id)kCGImageSourceThumbnailMaxPixelSize];
CGImageRef firstPage = CGImageSourceCreateImageAtIndex(src, 0, (CFDictionaryRef)options);
UIImage* imagePDF = [UIImage imageWithCGImage:firstPage];
CGImageRelease(firstPage);

我的 pdf 文件没问题,但是当我尝试此代码时,图像没有出现,并且我收到以下消息:“: ImageIO: CGImageSourceCreateImageAtIndex 图像源参数为零”。看不懂..

我尝试了其他链接,但遇到了同样的问题..

有什么想法吗? 还有其他方法可以将我的 pdf 文件转换为图像吗?

非常感谢!

最佳答案

您使用了不正确的 API 来创建 URL,您需要 URLWithString:

CFURLRef urlRef = (CFURLRef)[NSURL URLWithString:@"http://uat.okitoo.fr/document/document/s595_506cb1811852f/o_specimen-page-seule.pdf"];

您当前使用的 API 用于获取本地文件系统上文件的 URL。

关于ios - CGImageSourceCreateImageAtIndex 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12718411/

相关文章:

php - 从 href 链接问题下载 pdf

pdf - 在 mupdf 中从右到左阅读 PDF

delphi - 将 OpenOffice Calc 文件导出为 PDF 时如何尊重打印区域?

iphone - 我的图像和缩略图应该使用 png 还是 jpg?

ios - 如何在 iOS 中以字符串格式保存的日期列上使用 order by 子句?

ios - 由于名称为 'NSInternalInconsistencyException' 的未捕获异常 'OverlayView' 而终止应用程序

iphone - .h 实现文件无法识别我的 UIViewController

c# - 简单的 Segue 在 Xamarin 中不起作用

ios - 将全局类应用于 UIImage

ios - 与 GPUImage 混合 : How to control the position of the second image on top of the first