У меня есть файл test.pdf
в моем приложении Bundle. Я запускаю тест в iPhone Simulator (iPad), и я не могу получить заполненный CGPDFDocumentRef. Я схожу с ума или что?!?CGPDFDocumentCreateWithURL дает мне 0x0 ноль
здесь кусок кода из примера приложения сделаны специально для этого (класс ViewController):
- (void)viewDidLoad {
[super viewDidLoad];
// below code to show files in app bundle...
NSString *path = [[NSBundle mainBundle] resourcePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = [[NSError alloc] init];
NSArray *directoryAndFileNames = [fm contentsOfDirectoryAtPath:path error:&error];
//just to make sure the file is there : (test.pdf)
for (NSString *ts in directoryAndFileNames) {
NSLog(ts); // NSLog confirms the file is there
}
//Function below is supposed to create the CDPDFDocumentRef
[self updatePageRef]; // try to get the CGPDFDocumentRef
}
-(BOOL)updatePageRef {
NSLog(@"Updating PAGE");
NSString *pdfStringURL = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
NSURL *pdfURL = [NSURL fileURLWithPath:pdfStringURL];
NSLog([pdfURL path]); // this is good
NSLog(@"File Exists = %d", [[NSFileManager defaultManager] fileExistsAtPath:pdfStringURL]); // this gives me 1 (YES)
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
//from here on the pdf is nil??????????!!!!!!!!!!!!!
if (pdf && CGPDFDocumentGetNumberOfPages(pdf) > 0) {
return YES;
}
return NO;
}
Над pdf
объявлен в @interface, как CGPDFDocumentRef pdf;
Я знаю, что файл PDF хорош, потому что я успешно открыл его в «предварительном просмотре».
Все компилируется и запускается без каких-либо предупреждений и ошибок.
Помощь!
Почему вы создаете пустой объект ошибки? Вам просто нужно передать указатель на переменную где 'contentsOfDirectoryAtPath: error:' должен хранить объект ошибки * он * создает, если он терпит неудачу. –
Вы абсолютно правы. Кусок кода, показывающий файлы в комплекте, который я вставил откуда-то в сети. Однако ваш комментарий не приближает меня к решению моей проблемы. – Sasho