webview加载图片 gif pdf ppt txt doc 等

- (void)loadFile{

    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

    [webView.scrollView setScrollEnabled:YES];

    [webView setDelegate:self];

    if ([_fileName hasSuffix:@"png"] || [_fileName hasSuffix:@".jpg"] || [_fileName hasSuffix:@".jpeg"] || [_fileName hasSuffix:@".bmp"]) {

        [STHUDVIEW stopAnimating];

        UIImage *image = [UIImage imageWithData:_fileData];

        UIImageView *imageView = [[UIImageView alloc]initWithImage:image];

        

        [imageView setContentMode:UIViewContentModeScaleAspectFit];

        [imageView setAutoresizesSubviews:YES];

        [imageView setImage:image];

        

        if (imageView.frame.size.width > SCREEN_WIDTH) {

            [imageView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, imageView.frame.size.height)];

        }

        CGFloat topBtnGap = 0.0;


        if (imageView.frame.size.height > SCREEN_HEIGHT-PHONE_STATUSBAR_HEIGHT-PHONE_NAVIGATIONBAR_HEIGHT) {

            [imageView setFrame:CGRectMake(0, 0, imageView.frame.size.width, SCREEN_HEIGHT-PHONE_STATUSBAR_HEIGHT-PHONE_NAVIGATIONBAR_HEIGHT)];

        }else{

            topBtnGap = (SCREEN_HEIGHT-PHONE_STATUSBAR_HEIGHT-PHONE_NAVIGATIONBAR_HEIGHT - imageView.frame.size.height)/2;

        }

        

        CGFloat w = imageView.frame.size.width;

        CGFloat h = imageView.frame.size.height;

        

        CGFloat leftRightGap = (SCREEN_WIDTH - w)/2;

        [imageView setFrame:CGRectMake(leftRightGap,topBtnGap, w, h)];

        [self.view addSubview:imageView];

        ST_LOG(@"%@",_fileName);

        

    }else if([_fileName hasSuffix:@".gif"]){

        

        [webView loadData:_fileData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];

        [self.view addSubview:webView];

    }else if([_fileName hasSuffix:@".mp3"]){

        playAV = [[AVAudioPlayer alloc]initWithData:_fileData error:nil];

        [playAV setDelegate:self];

        [playAV prepareToPlay];

        [self.view addSubview:_progressView];

        

        [STHUDVIEW stopAnimating];

        

    }else if([_fileName hasSuffix:@".mp4"]){

        NSMutableString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

        NSString *newPath = [path stringByAppendingPathComponent:@"temp.mp4"];

        [_fileData writeToFile:newPath atomically:YES];

        NSURL *url = [[NSURL alloc]initFileURLWithPath:newPath];

        _playCtrl=[[MPMoviePlayerController alloc] initWithContentURL:url];

        _playCtrl.view.frame=CGRectMake(0, 0, 320, 460);

        [_playCtrl prepareToPlay];

        [self.view addSubview:_playCtrl.view];

        [_playCtrl play];

        [STHUDVIEW stopAnimating];

        

    }else if([_fileName hasSuffix:@".docx"] || [_fileName hasSuffix:@".pdf"] || [_fileName hasSuffix:@".txt"] || [_fileName hasSuffix:@".html"] || [_fileName hasSuffix:@".htm"] || [_fileName hasSuffix:@".doc"] || [_fileName hasSuffix:@".ppt"] || [_fileName hasSuffix:@".xlsx"] || [_fileName hasSuffix:@".xls"] || [_fileName hasSuffix:@".zip"]){

        if ([_fileName hasSuffix:@".docx"]) {

            [webView loadData:_fileData MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@"UTF-8" baseURL:nil];

            [self.view addSubview:webView];

        }else if ([_fileName hasSuffix:@".xlsx"]||[_fileName hasSuffix:@".xls"]) {

            NSMutableString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

            NSURL *url;

            NSURLRequest *request;

            NSString *newPath;

            if ([_fileName hasSuffix:@".xlsx"]) {

                newPath = [path stringByAppendingPathComponent:@"temp.xlsx"];

            }else{

                newPath = [path stringByAppendingPathComponent:@"temp.xls"];

            }

            [_fileData writeToFile:newPath atomically:YES];

            

            url = [NSURL fileURLWithPath:newPath];

            request = [NSURLRequest requestWithURL:url];

            [webView loadRequest:request];

            

            [self.view addSubview:webView];

            

            

        }else if ([_fileName hasSuffix:@".ppt"]) {

            [webView loadData:_fileData MIMEType:@"application/vnd.ms-powerpoint" textEncodingName:@"UTF-8" baseURL:nil];

            [self.view addSubview:webView];

        }else if ([_fileName hasSuffix:@".doc"]) {

            [webView loadData:_fileData MIMEType:@"application/msword" textEncodingName:@"UTF-8" baseURL:nil];

            [self.view addSubview:webView];

        }else if ([_fileName hasSuffix:@".pdf"]) {

            [webView loadData:_fileData MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil];

            [self.view addSubview:webView];

        }else if ([_fileName hasSuffix:@".txt"]) {

            NSMutableString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

            NSString *newPath = [path stringByAppendingPathComponent:@"temp.txt"];

            [_fileData writeToFile:newPath atomically:YES];

            [self.view addSubview:webView];

            

            ///编码可以解决 .txt 中文显示乱码问题

            NSStringEncoding *useEncodeing = nil;

            //带编码头的如utf-8等,这里会识别出来

            NSString *body = [NSString stringWithContentsOfFile:newPath usedEncoding:useEncodeing error:nil];

            //识别不到,按GBK编码再解码一次.这里不能先按GB18030解码,否则会出现整个文档无换行bug

            if (!body) {

                body = [NSString stringWithContentsOfFile:newPath encoding:0x80000632 error:nil];

            }

            //还是识别不到,按GB18030编码再解码一次.

            if (!body) {

                body = [NSString stringWithContentsOfFile:newPath encoding:0x80000631 error:nil];

            }

            

            //展现

            if (body) {

                [webView loadHTMLString:body baseURL: nil];

            }else {

                NSString *urlString = [[NSBundle mainBundle] pathForAuxiliaryExecutable:newPath];

                urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

                NSURL *requestUrl = [NSURL URLWithString:urlString];

                NSURLRequest *request = [NSURLRequest requestWithURL:requestUrl];

                [webView loadRequest:request];

            }

        }else if ([_fileName hasSuffix:@".html"] || [_fileName hasSuffix:@".htm"]) {

            [webView loadData:_fileData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];

            [self.view addSubview:webView];

        }else if([_fileName hasSuffix:@".zip"]){

            [self openZipFile:_fileData];

        }else{

            [STHUDVIEW stopAnimating];

        }

    }else{

        [STHUDVIEW stopAnimating];

        

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值