美文网首页
问题解决

问题解决

作者: 跬步千里_LenSky | 来源:发表于2016-08-17 15:20 被阅读28次

    - (id) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index

    {

    if ([self.fileURL.lastPathComponent rangeOfString:@".txt"].location != NSNotFound) {

    //处理txt格式内容显示有乱码的情况

    NSData *fileData = [NSData dataWithContentsOfURL:self.fileURL];

    //判断是UNICODE编码

    NSString *isUNICODE = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];

    //还是ANSI编码(-2147483623,-2147482591,-2147482062,-2147481296)encoding 任选一个就可以了

    NSString *isANSI = [[NSString alloc] initWithData:fileData encoding:-2147483623];

    if (isUNICODE) {

    NSString *retStr = [[NSString alloc]initWithCString:[isUNICODE UTF8String] encoding:NSUTF8StringEncoding];

    NSData *data = [retStr dataUsingEncoding:NSUTF16StringEncoding];

    [data writeToURL:self.fileURL atomically:YES];

    }else if(isANSI){

    NSData *data = [isANSI dataUsingEncoding:NSUTF16StringEncoding];

    [data writeToURL:self.fileURL atomically:YES];

    }

    }

    return self.fileURL;

    }

    相关文章

      网友评论

          本文标题:问题解决

          本文链接:https://www.haomeiwen.com/subject/lammsttx.html