首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIImagePNGRepresentation writeToFile可以捕获错误吗?

UIImagePNGRepresentation writeToFile可以捕获错误吗?
EN

Stack Overflow用户
提问于 2011-12-03 01:32:17
回答 1查看 2.5K关注 0票数 2

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

我使用了以下代码:

代码语言:javascript
复制
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]);
        }

控制台中没有错误,但PNG未存储到documents-Directory中。BOOL

代码语言:javascript
复制
writtenBool

是"0",这意味着写操作不成功。我也不知道原因。我将pngPath中的路径也写到了控制台,路径是正确的。如果我打开一个终端并写下

代码语言:javascript
复制
open <<copyed path from console>>

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

是什么原因导致此操作不起作用?我看了一下api,但似乎没有

代码语言:javascript
复制
error:

对于UIImagePNGRepresentation: writeToFile:

谢谢你的帮忙!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-03 02:04:09

UIImagePNGRepresentation(thumbnailImage)返回NSData对象

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

writeToFile:atomically:

writeToFile:options:error:

writeToURL:atomically:

writeToURL:options:error:

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8360273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档