本地缓存以及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;

}







0 0