【问题标题】:QuickLook/QLPreviewController(QLPreviewItem) change title in iOS8 in iOS7 works fineiOS7 中 iOS8 中的 QuickLook/QLPreviewController(QLPreviewItem) 更改标题工作正常
【发布时间】:2014-09-14 11:46:40
【问题描述】:

我正在尝试使用 QuickLook 库预览我的文档/图片。 当我想在 ios7 和 ios8 中打开内容时,一切都很好。 我想更改在预览中找到的项目的名称。 在 iOS7 上一切正常,但在 iOS8 上运行应用程序时出现问题。

这是我的代码:

-(void)startDownload{
// Download code here - after I set the  QLPreviewController

QLPreviewController* lcontroller= [QLPreviewController new];
self.quickLookUrl      = item.openUrl;
self.quickLookName     = item.name;
lcontroller.dataSource = self;
[self presentViewController:lcontroller animated:NO completion:nil];
}

#pragma mark - Quicklook Delegate
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
      return 1;
}

- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
    QLPreviewItemCustom *obj = [[QLPreviewItemCustom alloc] initWithTitle:self.quickLookName url:[NSURL fileURLWithPath:self.quickLookUrl]];
    return obj;
}

QLPreviewItemCustom - QLPreviewItem 详细信息:

QLPreviewItemCustom.h

#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>

@interface QLPreviewItemCustom : NSObject <QLPreviewItem>

- (id) initWithTitle:(NSString*)title url:(NSURL*)url;
@end

QLPreviewItemCustom.m - 详细信息:

   #import "QLPreviewItemCustom.h"

    @implementation QLPreviewItemCustom
    @synthesize previewItemTitle = _previewItemTitle;
    @synthesize previewItemURL   = _previewItemURL;

    - (id) initWithTitle:(NSString*)title url:(NSURL*)url
    {
        self = [super init];
        if (self != nil) {
            _previewItemTitle = title;
            _previewItemURL   = url;
        }
        return self;
    }
    @end

当我在 iOS8 上运行项目时,结果如下:

我已阅读有关 iOS8 GM 的发行说明(我能够在应用程序中打开 PDF 文件而没有任何问题): iOS8 GM Release Notes

此时不知道还是新SDK的问题还是我开发过程中的错误。 你能帮我提些建议吗?我还没有找到其他库来帮助我预览我的项目的内容(pdf、jpg、excel、doc..)。

提前感谢您的帮助!

【问题讨论】:

  • 请使用您的代码在bugreport.apple.com 提交错误。如果您不尝试更改项目的标题,是否也会发生这种情况?
  • 谢谢!我会完成文件报告。
  • 我在使用 RTF 文件时遇到了同样的问题,有什么消息吗?
  • @OliverM 嗨,你明白了吗,因为我正在尝试使用 QLPreviewController 打开下载的 .xls 文件,但它显示的格式相同。如果是,请帮助我解决这个问题。
  • 嗯......我认为这是一个模拟器唯一的问题。在设备上进行了测试,并且可以正常工作。

标签: ios ios7 ios8 quicklook qlpreviewcontroller


【解决方案1】:

我认为您忘记在代码中指明您的 QLPreviewController 的委托。我在 iOS8 上运行此代码,它工作正常:

// MARK: - QLPreviewControllerDelegate

func numberOfPreviewItemsInPreviewController(controller: QLPreviewController! ) -> Int
{
    return 1
}


func previewController(controller: QLPreviewController!, previewItemAtIndex index: Int) -> QLPreviewItem!
{
    // Break the path into it's components (filename and extension)
    // Use the filename (index 0) and the extension (index 1) to get path
    let path = self.filenameToShow
    return NSURL.fileURLWithPath(path!)!
}


func showPrescription(sender: UIButton)
{

    self.previewController = IGQLPreviewController()
    let directory  = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    var path: String = directory[0] as! String
    let fileName = String(format: "%@_%@.png", arguments: [self.numberTextField.text, self.name.text])
    let imagePath = path.stringByAppendingPathComponent(fileName)

    self.filenameToShow = imagePath

    self.previewController!.dataSource = self
    self.previewController!.delegate = self
    self.previewController!.currentPreviewItemIndex = 0

    self.navigationController?.pushViewController(self.previewController!, animated: true)

}

【讨论】:

    【解决方案2】:
    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
    
            //NSURL(fileURLWithPath: self.imageUrl) this will append 'file' string at the end, if you try to load file directly from url.
            // so use below code to load file directly from url.
            let doc = URL(string: fileUrl)
            return doc! as QLPreviewItem
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多