高仿微博圖片瀏覽器

Lina07N 8年前發布 | 5K 次閱讀 iOS開發 移動開發

KNPhotoBrower

高仿 微博 圖片瀏覽器

一.功能描述及要點

  • 1.加載網絡九宮格圖片,collectionView,scrollView
  • 2.SDWebImage下載圖片,KNProgressHUD顯示加載進度
  • 3.高仿微博,顯示動畫,KNToast提示

二.方法調用

1.創建KNPhotoBrower,并傳入相應的參數

// 每一個圖片控件對象, 對一一對應 KNPhotoItems ,再將多個KNPhotoItems 對象放入數組
KNPhotoItems *items = [[KNPhotoItems alloc] init];
items.url = [urlArr[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
items.sourceView = imageView;

KNPhotoBrower photoBrower = [[KNPhotoBrower alloc] init]; photoBrower.itemsArr = [_itemsArray copy];// KNPhotoItems對象的數組 photoBrower.currentIndex = tap.view.tag;// 當前點擊的哪個圖片 photoBrower.actionSheetArr = [self.actionSheetArray mutableCopy];//設置 ActionSheet的選項 [photoBrower present];// 顯示</code></pre>

2.提供代理方法 --> KNPhotoBrowerDelegate

/ PhotoBrower 即將消失 */

  • (void)photoBrowerWillDismiss; / PhotoBrower 右上角按鈕的點擊 /
  • (void)photoBrowerRightOperationActionWithIndex:(NSInteger)index; / PhotoBrower 保存圖片是否成功 /
  • (void)photoBrowerWriteToSavedPhotosAlbumStatus:(BOOL)success;</code></pre>

    3.提供 消失方法

    [_photoBrower dismiss];

    4.設置 參數

    /**
    • 是否需要右上角的按鈕. Default is YES; */ @property (nonatomic, assign) BOOL isNeedRightTopBtn; /**
    • 是否需要 頂部 1 / 9 控件 ,Default is YES */ @property (nonatomic, assign) BOOL isNeedPageNumView; /**
    • 是否需要 底部 UIPageControl, Default is NO */ @property (nonatomic, assign) BOOL isNeedPageControl; /**
    • 存放 ActionSheet 彈出框的內容 :NSString類型 / @property (nonatomic, strong) NSMutableArray actionSheetArr;</code></pre>

      5.關于彈出框的內容,可在KNPhotoBrower.m 的operationBtnIBAction 方法中增減

      #pragma mark - 右上角 按鈕的點擊
  • (void)operationBtnIBAction{ __weak typeof(self) weakSelf = self;

    if(_actionSheetArr.count != 0){ // 如果是自定義的 選項

      KNActionSheet *actionSheet = [[KNActionSheet alloc] initWithCancelBtnTitle:nil destructiveButtonTitle:nil otherBtnTitlesArr:[_actionSheetArr copy] actionBlock:^(NSInteger buttonIndex) {
    
          // 讓代理知道 是哪個按鈕被點擊了
          if([weakSelf.delegate respondsToSelector:@selector(photoBrowerRightOperationActionWithIndex:)]){
              [weakSelf.delegate photoBrowerRightOperationActionWithIndex:buttonIndex];
          }

warning 如果傳入的 ActionSheetArr 有下載圖片這一選項. 則在這里調用和下面一樣的方法 switch.....,如果沒有下載圖片,則通過代理方法去實現... 目前不支持刪除功能

    }];
    [actionSheet show];
}else{
    KNActionSheet *actionSheet = [[KNActionSheet alloc] initWithCancelBtnTitle:nil destructiveButtonTitle:nil otherBtnTitlesArr:@[@"保存圖片",@"轉發微博",@"贊"] actionBlock:^(NSInteger buttonIndex) {

        // 讓代理知道 是哪個按鈕被點擊了
        if([weakSelf.delegate respondsToSelector:@selector(photoBrowerRightOperationActionWithIndex:)]){
            [weakSelf.delegate photoBrowerRightOperationActionWithIndex:buttonIndex];
        }

        switch (buttonIndex) {
            case 0:{
                SDWebImageManager *mgr = [SDWebImageManager sharedManager];
                KNPhotoItems *items = _itemsArr[_currentIndex];
                if(![mgr diskImageExistsForURL:[NSURL URLWithString:items.url]]){
                    [[KNToast shareToast] initWithText:@"圖片需要下載完成"];
                    return ;
                }else{
                    UIImage *image = [[mgr imageCache] imageFromDiskCacheForKey:items.url];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
                    });
                }
            }
            default:
                break;
        }
    }];
    [actionSheet show];
}

}</code></pre>

補充

  • 1.目前適合 九宮格樣式,collectionView,scrollView

  • 2.如果有bug, 請在Github上通過 '郵箱' 或者 直接issue ,我會盡快修改

 

來自:http://www.jianshu.com/p/6065684b3ad2

 

 本文由用戶 Lina07N 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!