ios - UIImagePNGRepresentation writeToFile 可以捕获错误吗?

标签 ios png writetofile

我将新收到的 PDF 文档的缩略图呈现到文档目录中。

我正在使用以下代码:

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("thedoc.pdf"), NULL, NULL);
        CGPDFDocumentRef bookPDF = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
        UIGraphicsBeginImageContext(CGSizeMake(100, 130));
        NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(bookPDF);
        CGContextRef context = UIGraphicsGetCurrentContext();

        for(int i = 0; i < totalNum; i++ ) {
            CGRect arect = CGRectMake(0,0,200,282);
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, 0.0, 141);
            CGContextScaleCTM(context, 1.0, -1.0);
            CGContextSetGrayFillColor(context, 1.0, 1.0);
            CGContextFillRect(context, arect);
            // Grab the first PDF page
            CGPDFPageRef page = CGPDFDocumentGetPage(bookPDF, i + 1);
            CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, arect, 0, true);
            // And apply the transform.
            CGContextConcatCTM(context, pdfTransform);
            CGContextDrawPDFPage(context, page);
            // Create the new UIImage from the context
            UIImage* thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
            if (thumbnailImage == nil) {
                NSLog(@"ERROR during creation of PNG");
            }
            // SAVE THE IMAGE TO DOCUMENTS-DIRECTORY
            NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/thumbPage%i.png",theLocalFolder ,i]];
            // Write image to PNG
            BOOL writtenBool = [UIImagePNGRepresentation(thumbnailImage) writeToFile:pngPath atomically:YES];
            // END SAVE THE IMAGE TO DOCUMENT-DIRECTORY
            CGContextRestoreGState(context);
            NSLog(@"Rendering PDF-Thumbnail %i (%i): %@", i, writtenBool, [NSString stringWithFormat:@"%@/thumbPage%i.png",theLocalFolder ,i]);
        }

Console 中没有错误,但 PNG 未存储到 documents-Directory 中。 bool

writtenBool

为“0”,表示写操作不成功。我不知道为什么。我把路径写在 png路径 也到控制台,路径是正确的。如果我打开终端并写入

open <<copyed path from console>>

它会在 finder 中打开正确的路径。

什么会导致它不起作用?我查看了 api,但似乎没有

error:

对于 UIImagePNGRepresentation:writeToFile:

感谢您的帮助!

最佳答案

UIImagePNGRepresentation(thumbnailImage) 返回一个 NSData 对象

对于NSData对象,你可以使用这些方法:

writeToFile:atomically:,

writeToFile:options:error:,

writeToURL:atomically:,

writeToURL:options:error:,

因此,您可以尝试使用类似 BOOL writtenBool = [UIImagePNGRepresentation(thumbnailImage) writeToFile:pngPath options:NSDataWritingAtomic error:&error]; 的代码来查看发生了什么。

关于ios - UIImagePNGRepresentation writeToFile 可以捕获错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8360273/

相关文章:

ios - writeToFile:atomically: 方法追加到当前现有文件?

cocoa - 将文件名与文件扩展名 Cocoa 分开?

Matlab 将数字元胞数组写入文件

ios - 使用 https 在 iOS native 应用程序的 fiddler 中记录问题

ios - 插入耳机时奇怪的 AVMutableComposition 行为

ios - 界面生成器中的选项卡栏图标太大

iphone - 为什么 viewDidAppear/viewWillAppear 不会在嵌套结构中被调用?

android - 如何从 Android 中的动态文本制作 png 图像?

c - 读取缓冲区中的 png 图像

python - SVG/PNG 文件在浏览器中的打开方式不同