QLPreviewController使用方法

在iOS SDK包括了QLPreviewControllerAPI在[iOS]中用于方便預(yù)覽文件,它支持的文件有:

  • iWork文檔
  • 微軟Office97以上版本的文檔
  • RTF文檔
  • PDF文件
  • 圖片文件
  • 文本文件和CSV文件

使用的時(shí)候首先要引用#import。#import <QuickLook/QuickLook.h>
QLPreviewController類似于Tableview的使用方法,也是首先遵循代理和數(shù)據(jù)源代理。然后實(shí)現(xiàn)代理方法,<QLPreviewControllerDataSource,QLPreviewControllerDelegate>
我返回的PreviewItems的數(shù)量為1,就是一次加載一個(gè)文件,這里可以是多個(gè)文件的數(shù)組個(gè)數(shù),也就是某個(gè)本地路徑下的多個(gè)文件,但是return 不能直接寫等于1 ,這樣會(huì)沒有效果,應(yīng)該寫self.arr.count;大家可以自己實(shí)現(xiàn)下。

-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
    return 1;
}

返回的當(dāng)前預(yù)覽的文件QLPreviewItem,controller:當(dāng)前預(yù)覽控制器,index:當(dāng)前預(yù)覽的第幾個(gè)文件。

- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx{
    ContractDetailModel *modelNew = _arr[idx];
    return [NSURL fileURLWithPath:[self getApplicationDocumentsDirectory:modelNew]];
}

這里為什么返回Url呢,進(jìn)入QLPreviewItem頭文件你就會(huì)明白了:

QL_EXPORT @protocol QLPreviewItem <NSObject>
@required
/*!
 * @abstract The URL of the item to preview.
 * @discussion The URL must be a file URL. 
 */
@property(readonly, nonnull, nonatomic) NSURL * previewItemURL;
@optional
/*!
 * @abstract The item's title this will be used as apparent item title.
 * @discussion The title replaces the default item display name. This property is optional.
 */
@property(readonly, nullable, nonatomic) NSString * previewItemTitle;
@end
/*!
 * @abstract This category makes NSURL instances as suitable items for the Preview Controller.
 */
@interface NSURL (QLPreviewConvenienceAdditions) <QLPreviewItem>

QLPreviewControlle其他的一些方法

/*!
 * @abstract Returns YES if QLPreviewController can display this preview item.摘要返回YES如果QLPreviewController能顯示預(yù)覽項(xiàng)目。
 */
+ (BOOL)canPreviewItem:(id <QLPreviewItem>)item;

/*
 * Acessing the previewed items*數(shù)據(jù)預(yù)覽項(xiàng)目
 */

/*!
 * @abstract The Preview Panel data source.抽象預(yù)覽面板數(shù)據(jù)源。
 */
@property(nonatomic, weak, nullable) id <QLPreviewControllerDataSource> dataSource;

/*!
 * @abstract Asks the Preview Controller to reload its data from its data source.抽象要求預(yù)覽控制器從其數(shù)據(jù)源重新加載數(shù)據(jù)。
 * @discussion This method does not refresh the visible item if it has not changed.刷新可見項(xiàng),。
 */
- (void)reloadData;

/*!
 * @abstract Asks the Preview Controller to recompute the preview of the currently previewed item.刷新當(dāng)前預(yù)覽項(xiàng)目。
 */
- (void)refreshCurrentPreviewItem;

/*!
 * @abstract The index of the currently previewed item in the preview panel or NSNotFound if there is none.當(dāng)前預(yù)覽項(xiàng)目的下標(biāo)。
 */
@property NSInteger currentPreviewItemIndex;

/*!
 * @abstract The currently previewed item in the preview panel or nil if there is none.當(dāng)前預(yù)覽項(xiàng)目。
 */
@property(readonly, nullable) id <QLPreviewItem> currentPreviewItem;

監(jiān)聽滑到的是第幾個(gè)瀏覽項(xiàng)目,做出相應(yīng)的操作
如下是改變了title

[self.previewController addObserver:self forKeyPath:@"currentPreviewItemIndex" options:
     NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
//keyPath:屬性名稱
//object:被觀察的對(duì)象
//change:變化前后的值都存儲(chǔ)在 change 字典中
//context:注冊(cè)觀察者時(shí),context 傳過來(lái)的值
/* 2.只要object的keyPath屬性發(fā)生變化,就會(huì)調(diào)用此回調(diào)方法,進(jìn)行相應(yīng)的處理:UI更新:*/
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                       change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    // 判斷是否為self.myKVO的屬性“num”:
    if([keyPath isEqualToString:@"currentPreviewItemIndex"] && object == self.previewController) {
        // 響應(yīng)變化處理:UI更新(label文本改變)
        
        NSString *num = [NSString stringWithFormat:@"%@",
                           [change valueForKey:@"new"]];
        NSInteger number = [num integerValue];
        ContractDetailModel *model = _arr[number];
        if ([model.agreementType isEqualToString:@"0"] ) {
            self.title = @"title1";
        } else if ([model.agreementType isEqualToString:@"1"]){
            self.title = @"title2";
        }
        if ([num isEqualToString:@"0"]) {
            [self.firstImg setImage:[UIImage imageNamed:@"Oval91"]];
            [self.secsonImg setImage:[UIImage imageNamed:@"Oval92"]];
            
        } else if ([num isEqualToString:@"1"]) {
            [self.firstImg setImage:[UIImage imageNamed:@"Oval92"]];
            [self.secsonImg setImage:[UIImage imageNamed:@"Oval91"]];
        }
        //change的使用:上文注冊(cè)時(shí),枚舉為2個(gè),因此可以提取change字典中的新、舊值的這兩個(gè)方法
        NSLog(@"\\noldnum:%@ newnum:%@",[change valueForKey:@"old"],
              [change valueForKey:@"new"]);
    }
}

剛剛接觸這塊,如有錯(cuò)誤,也請(qǐng)指出,謝謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容