本地缓存以及UIDocumentInteractionController的使用
来源:互联网 发布:电脑编程有学校吗 编辑:程序博客网 时间:2024/06/06 01:41
本地数据缓存的优点主要就说两个啦,1.数据提取快,用户体验好;2.节约用户的流量
项目数据拉取量很大,拉取速度慢,要想用户跳转进入某个页面已经具有数据的展示就要求具有缓存的功能(一进去就是一个菊花转来转去或者一片空白等数据传完谁都想换app)
原理不想说,直接进入正题:
首先,功能需求很简单,在一个ViewController中显示一个图片,PDF图片或者其他的诸如png格式的图片,点击图片,图片有个“溢出”屏幕的效果,支持手势的放大缩小,或者双击放大双击缩小等;
思路很简单,用一个containerview来装载图片(如果是PDF文件或者nsdata则要专门的转PDF格式显示)--->在图片上增加一个pan手势操作--->点击时调取苹果的document API--->显示时其他所有图片的操作苹果已经帮你弄好了;
直接上代码:
1.首先是图片数据的缓存:
#pragma mark - containerView
_PDFView = [[UIViewalloc] initWithFrame:CGRectZero];
_PDFView.backgroundColor = [UIColorwhiteColor];
[scrollViewaddSubview:_PDFView];
[_PDFViewmas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.top.equalTo(scrollView).offset(WIDTH_SCALE_REFERTO_414(12));
make.trailing.equalTo(scrollView).offset(WIDTH_SCALE_REFERTO_414(-12));
make.height.offset(WIDTH_SCALE_REFERTO_414(516));
}];
#pragma mark - add gesture*** into containerView and link the action with openFile
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(openFile)];
[_PDFViewaddGestureRecognizer:tap1];
#pragma mark - 这里用服务端返回的PDF base64string来举例,其中用第三方的库来解析PDF数据
https://gist.github.com/1209911
//获取base64位编码字符串
NSString *w8Str = [serviceDic objectForKey:@"w8"];
//转成NSDate
if (w8Str.length >0) {
NSData *data = [[NSDataalloc]initWithBase64EncodedString:w8Stroptions:0];
UIImage *image = [UIImageimageWithPDFData:datatSize:self.pdfImageView.frame.size];
//写进缓存
_path = [selffilePathAtDocumentWithFileName:@"UserPDFImage.pdf"];
[data writeToFile:_pathatomically:YES];
self.pdfImageView.image = image;
}
#pragma mark - 获取本地缓存路径
-(NSString *)cachePath
{
return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES)
objectAtIndex:0];
}
#pragma mark -在本地缓存路径后加上文件的名字(用字符串的拼接方法).这样使用的返回路径就可以找到指定文件,文件的读取和写入便能够顺利完成了.
- (NSString *)filePathAtDocumentWithFileName:(NSString *)fileName
{
NSString *filePath = [[selfcachePath] stringByAppendingPathComponent:fileName];
return filePath;
}
#pragma mark - 打开文件的action-(void)openFile{
NSURL *urlStr;
urlStr = [NSURLfileURLWithPath:[selffilePathAtDocumentWithFileName:@"UserPDFImage.pdf"]];
if (urlStr) {
_doucmentVC = [UIDocumentInteractionControllerinteractionControllerWithURL:urlStr];
_doucmentVC.delegate =self;
[_doucmentVCpresentPreviewAnimated:YES];
// [_doucmentVC presentOpenInMenuFromRect:_PDFView.frame inView:self.view animated:YES];//选择手机中其他app打开
}
}
#pragma mark -- UIDocumentInteractionControllerDelegate
- (void)presentPreview
{
[_doucmentVCpresentPreviewAnimated:YES];
}
- (void)presentOpenInMenu
{
[_doucmentVCpresentOpenInMenuFromRect:self.view.boundsinView:self.viewanimated:YES];
}
- (void)presentOptionsMenu
{
[_doucmentVCpresentOptionsMenuFromRect:self.view.boundsinView:self.viewanimated:YES];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
return self.view.frame;
}
- 本地缓存以及UIDocumentInteractionController的使用
- UIDocumentInteractionController 的使用
- UIDocumentInteractionController 的使用
- UIDocumentInteractionController 的使用
- XListView的使用以及本地缓存
- SDWebImage使用以及缓存和本地存储的清理
- 本地缓存的使用
- 使用第三方APP打开本地文档 (UIDocumentInteractionController)
- UIDocumentInteractionController的使用之打开和预览文档
- UIDocumentInteractionController
- gradle 使用 maven的本地缓存仓库
- 本地缓存的实现以及遇到的问题
- android LruCache的使用 (本地缓存+内存缓存)
- 利用UIDocumentInteractionController文档预览以及调用iPhone里第三方相关的app打开文档
- 缓存-ehcach使用以及simplepagecache的使用
- Guava本地缓存托底缓存以及异步更新缓存
- 各级缓存的使用场景以及限制
- 本地缓存的机制
- NotificationCenter
- linux设备驱动模型
- 腾讯实习生面试
- #include<bits/stdc++.h>包含C++的所有头文件
- 马的走法
- 本地缓存以及UIDocumentInteractionController的使用
- Laravel数据库迁移文件migrate使用约定
- 更新gradle插件,提升编译速度
- java之servlet
- 2016SDAU编程练习二1009
- {题解}[jzoj4326]NOIP2015提高组Day2 跳石头
- 2.6 Binder机制(MediaService)
- 栈溢出笔记1.9 认识SEH
- C#使用ICSharpCode.SharpZipLib压缩后进行web批量下载文件