【问题标题】:UIImagePNGRepresentation writeToFile can errors be catched?UIImagePNGRepresentation writeToFile 可以捕获错误吗?
【发布时间】:2012-01-11 17:10:00
【问题描述】:

我将新收到的 PDF 文档的缩略图渲染到 Documents-Directory。

我正在使用以下代码:

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 未存储到文档目录中。布尔值

writtenBool

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

open <<copyed path from console>>

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

什么可能导致这不起作用?我看了一下api,但似乎没有

error:

对于 UIImagePNGRepresentation:writeToFile:

感谢您的帮助!

【问题讨论】:

    标签: ios png writetofile


    【解决方案1】:

    UIImagePNGRepresentation(thumbnailImage) 返回一个NSData 对象

    对于NSData 对象,可以使用以下方法:

    writeToFile:atomically:,

    writeToFile:options:error:,

    writeToURL:atomically:,

    writeToURL:options:error:,

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

    【讨论】:

      猜你喜欢
      • 2011-09-15
      • 2019-08-25
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多