今天要实现一个功能, 通过iTunes导入文件到应用中, 并且在应用中对这个文件进行编辑。
类似我们平时经常使用的 PDF阅读器那样的东西, 我们可以自己导入我们的电子书。
源码下载:https://github.com/colin1994/iTunesTest.git
下面具体介绍下实现过程。
先看效果图。
图1. 未实现功能前, iTunes截图
]; NSData *jpgData = UIImageJPEGRepresentation(image, 0.8); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0]; NSString *filePath = [documentsPath stringByAppendingPathComponent:@"testPic.jpg"]; [jpgData writeToFile:filePath atomically:YES]; char *saves = "Colin_csdn"; NSData *data = [[NSData alloc] initWithBytes:saves length:10]; filePath = [documentsPath stringByAppendingPathComponent:@"colin.txt"]; [data writeToFile:filePath atomically:YES]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDir = [documentPaths objectAtIndex:0]; NSError *error = nil; NSArray *fileList = [[NSArray alloc] init]; fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error]; self.dirArray = [[NSMutableArray alloc] init]; for (NSString *file in fileList) { [self.dirArray addObject:file]; } [readTable reloadData];}- (void)setupDocumentControllerWithURL:(NSURL *)url{ if (self.docInteractionController == nil) { self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url]; self.docInteractionController.delegate = self; } else { self.docInteractionController.URL = url; }}#pragma mark- 列表操作- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellName = @"CellName"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellName]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellName]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } NSURL *fileURL= nil; NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDir = [documentPaths objectAtIndex:0]; NSString *path = [documentDir stringByAppendingPathComponent:[self.dirArray objectAtIndex:indexPath.row]]; fileURL = [NSURL fileURLWithPath:path]; [self setupDocumentControllerWithURL:fileURL]; cell.textLabel.text = [self.dirArray objectAtIndex:indexPath.row]; NSInteger iconCount = [self.docInteractionController.icons count]; if (iconCount > 0) { cell.imageView.image = [self.docInteractionController.icons objectAtIndex:iconCount - 1]; } return cell;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.dirArray count];}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ QLPreviewController *previewController = [[QLPreviewController alloc] init]; previewController.dataSource = self; previewController.delegate = self; previewController.currentPreviewItemIndex = indexPath.row; [[self navigationController] pushViewController:previewController animated:YES]; }#pragma mark - UIDocumentInteractionControllerDelegate- (NSString *)applicationDocumentsDirectory{ return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];}- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController{ return self;}#pragma mark - QLPreviewControllerDataSource- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController{ return 1;}- (void)previewControllerDidDismiss:(QLPreviewController *)controller{ }- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx{ NSURL *fileURL = nil; NSIndexPath *selectedIndexPath = [readTable indexPathForSelectedRow]; NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDir = [documentPaths objectAtIndex:0]; NSString *path = [documentDir stringByAppendingPathComponent:[self.dirArray objectAtIndex:selectedIndexPath.row]]; fileURL = [NSURL fileURLWithPath:path]; return fileURL;}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; }@end
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow