iOS7中的QuickLook / QLPreviewController(QLPreviewItem)更改标题工作正常

时间:2014-09-14 11:46:41

标签: ios ios7 ios8 quicklook qlpreviewcontroller

我尝试使用QuickLook库预览文档/图片。    当我想在ios7和ios中打开内容时,一切都很好。 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上运行项目时,结果如下:

enter image description here

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

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

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我认为您忘记在代码中指明您的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)

}

答案 1 :(得分:-1)

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

    }