IOS 预览功能(轻松实现对各种文本、图片等查看)

首先需要#import
然后实现协议
别的不多说上代码

-(NSURL *)urlForFile:(NSString *)file
{
NSArray *segments=[file componentsSeparatedByString:@"."];
NSString *path=[[NSBundle mainBundle] pathForResource:[segments objectAtIndex:0] ofType:[segments objectAtIndex:1]];
return [NSURL fileURLWithPath:path];
}

-(void)viewDidLoad
{
[super viewDidLoad];
files=[NSArray alloc]initWithObjects:@"grocery.pdf",@"index.html",@"newsletter.pages",@"plain.txt",@"smile.jpg",nil];
}

#pragma mark Quick Look Data Source 

- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {

return [files count];

}


- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index {

return [self urlForFile:[files objectAtIndex: index]];

}

 (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

{

QLPreviewController * ql = [[[QLPreviewController alloc] init] autorelease];

ql.dataSource = self;

ql.currentPreviewItemIndex = indexPath.row;

[self.navigationController pushViewController:ql animated:YES];

}

你可能感兴趣的:(IOS)