我在這裏創建PDF和PDF的URL。我的問題是我試圖返回PDF的NSData,但當我嘗試在該方法的結尾它返回零。爲什麼它返回零?我怎樣才能解決這個問題?Objective-C NSData返回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 valueForKey:object];
}
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;
}
UPDATE
我加入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"}}
該文件是否存在'fileURL'?注意:絕對文件URL必須比'myString'更好。 – zaph
如何使用'dataWithContentsOfFile:options:error:'版本的調用,以便您可以看到錯誤是什麼? –
你有沒有嘗試過調試的任何部分?什麼是'fileURL'?它存在嗎? –