• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - Objective-C NSData 返回 nil

[复制链接]
菜鸟教程小白 发表于 2022-12-13 11:50:37 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我这里有这个方法,它可以创建 PDF 和 PDF 的 URL。我的问题是我正在尝试返回 PDF 的 NSData,但是当我尝试在方法结束时它返回 nil。为什么它返回 nil ?我该如何解决这个问题?

- (NSData *) setProductionSchedulePDF
{
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filename = @"test.pdf";
    NSURL *fileURL = [NSURL fileURLWithPathComponents:[NSArray arrayWithObjects:documentsDirectory, filename, nil]];

    CGContextRef pdfContext = CGPDFContextCreateWithURL((CFURLRef)fileURL, NULL, NULL);
    CGPDFContextBeginPage(pdfContext, NULL);
    UIGraphicsPushContext(pdfContext);

    CGRect bounds = CGContextGetClipBoundingBox(pdfContext);
    CGContextScaleCTM(pdfContext, 1.0, -1.0);
    CGContextTranslateCTM(pdfContext, 0.0, -bounds.size.height);

    NSArray *taskArray = self.taskData;

    UIFont *headerFont = [UIFont fontWithName"Arial" size:8];

    UIFont *font = [UIFont fontWithName"Arial" size:6];

    NSDictionary *attributes = @{ NSFontAttributeName: font};

    NSDictionary *headerAttributes = @{ NSFontAttributeName: headerFont};

    [@"Task" drawAtPoint:CGPointMake(10, 10) withAttributes:headerAttributes];

    int counter = 0;
    for (id object in taskArray) {
        [object drawAtPoint:CGPointMake(10, 10.5 * counter + 30) withAttributes:attributes];
        counter++;
    }

    int keyCounter = 0;
    for(id object in self.prodSchedSortedKeys) {
        [object drawAtPoint:CGPointMake(60 * keyCounter + 90, 10) withAttributes:headerAttributes];
        NSArray *bfsmArray;

        if([finalProductionSchedule count]!=0)
        {
            bfsmArray = [finalProductionSchedule valueForKeybject];
        }
        else
        {
            bfsmArray = nil;
        }

        bfsmArray = nil;

        int valueCounter = 0;
        for(id object2 in bfsmArray)
        {
            if(bfsmArray != nil)
            {

                if(valueCounter == 13 || valueCounter == 66 || valueCounter == 67){

                    [[bfsmArray objectAtIndex:valueCounter] drawAtPoint:CGPointMake(60 * keyCounter + 90, 10.5 * valueCounter + 30) withAttributes:attributes];

                }
                else if (valueCounter == 68 || valueCounter == 69)
                {
                    [[[bfsmArray objectAtIndex:valueCounter] baseLineStart] drawAtPoint:CGPointMake(60 * keyCounter + 90, 10.5 * valueCounter + 30) withAttributes:attributes];
                }
                else
                {
                    if([[[bfsmArray objectAtIndex:valueCounter] baseLineStart] isEqualToString""] && [[[bfsmArray objectAtIndex:valueCounter] actualFinish] isEqualToString""]){

                    }
                    else
                    {
                        [[NSString stringWithFormat"%@ / %@",[[bfsmArray objectAtIndex:valueCounter] baseLineStart],[[bfsmArray objectAtIndex:valueCounter] actualFinish]] drawAtPoint:CGPointMake(60 * keyCounter + 90, 10.5 * valueCounter + 30) withAttributes:attributes];
                    }
                }

            }
            NSLog(@"%@", object2);
            valueCounter++;
        }
        keyCounter++;
    }

    UIGraphicsPopContext();
    CGPDFContextEndPage(pdfContext);
    CGPDFContextClose(pdfContext);

    NSString *myString = [fileURL absoluteString];

    NSData *pdfData = [NSData dataWithContentsOfFile:myString];

    return pdfData;
}

更新

我像这样添加了 NSError:

NSError *error;
    NSData *pdfData = [NSData dataWithContentsOfFile:myString options:nil error:&error];

    NSLog(@"%@", error);

并且 NSError 返回以下内容:

error   NSError *   domain: @"NSCocoaErrorDomain" - code: 260   0x16820310

这是错误的控制台日志:

Error Domain=NSCocoaErrorDomain Code=260 "The file “test.pdf” couldn’t be opened because there is no such file." UserInfo={NSFilePath=file:///var/mobile/Containers/Data/Application/A2C316BA-97DF-4560-8F6D-BA6A15B2CCA3/Documents/test.pdf, NSUnderlyingError=0x155420e0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}



Best Answer-推荐答案


变化:

NSString *myString = [fileURL absoluteString];

收件人:

NSString *myString = [fileURL path];

原因是 [NSData dataWithContentsOfFile:] 需要一个路径,即 /some/dir/file.pdfabsoluteString 返回一个 URL,即:file://some/dir/file.pdf

您可以在 question 中阅读更多关于 pathabsoluteString 之间区别的信息。 .

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap