QBImagePickerController 用法

梁丘诚
2023-12-01
//
//  ViewController.m
//  QBImagePickerControllerDemo
//
//  Created by Tanaka Katsuma on 2013/12/30.
//  Copyright (c) 2013年 Katsuma Tanaka. All rights reserved.
//

#import "ViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (![QBImagePickerController isAccessible]) {
        NSLog(@"Error: Source is not accessible.");
    }
}


#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QBImagePickerController *imagePickerController = [[QBImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsMultipleSelection = (indexPath.section == 1);
    
    if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 1:
                imagePickerController.minimumNumberOfSelection = 3;
                break;
                
            case 2:
                imagePickerController.maximumNumberOfSelection = 6;
                break;
                
            case 3:
                imagePickerController.minimumNumberOfSelection = 3;
                imagePickerController.maximumNumberOfSelection = 6;
                break;
                
            default:
                break;
        }
    }
    
    if (indexPath.section == 0 && indexPath.row == 1) {
        [self.navigationController pushViewController:imagePickerController animated:YES];
    } else {
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:imagePickerController];
        [self presentViewController:navigationController animated:YES completion:NULL];
    }
}

- (void)dismissImagePickerController
{
    if (self.presentedViewController) {
        [self dismissViewControllerAnimated:YES completion:NULL];
    } else {
        [self.navigationController popToViewController:self animated:YES];
    }
}


#pragma mark - QBImagePickerControllerDelegate

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAsset:(ALAsset *)asset
{
    NSLog(@"*** qb_imagePickerController:didSelectAsset:");
    NSLog(@"%@", asset);
    [self dismissImagePickerController];
}

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets
{
    NSLog(@"*** qb_imagePickerController:didSelectAssets:");
    NSLog(@"%@", assets);
    
    [self dismissImagePickerController];
}

- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController
{
    NSLog(@"*** qb_imagePickerControllerDidCancel:");
    
    [self dismissImagePickerController];
}

@end

启动测试程序 会有一个闪退 需要修改 QBAssetsCollectionViewController.m

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionFooter) {
        QBAssetsCollectionFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
                                                                                      withReuseIdentifier:@"FooterView"
                                                                                             forIndexPath:indexPath];
        
        switch (self.filterType) {
            case QBImagePickerControllerFilterTypeNone:{
                NSString *format;
                if (self.numberOfPhotos == 1) {
                    if (self.numberOfVideos == 1) {
                        format = @"format_photo_and_video";
                    } else {
                        format = @"format_photo_and_videos";
                    }
                } else if (self.numberOfVideos == 1) {
                    format = @"format_photos_and_video";
                } else {
                    format = @"format_photos_and_videos";
                }
                NSLog(@"%@",NSLocalizedStringFromTableInBundle(format,\
                                                               @"QBImagePickerController",\
                                                               [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"QBImagePickerController" ofType:@"bundle"]],\
                                                               nil));
                
                footerView.textLabel.text = [NSString stringWithFormat:@"%ld Photo, %ld Video",
                                             (unsigned long)self.numberOfPhotos,\
                                             (unsigned long)self.numberOfVideos\
                                             ];
                break;
            }

            case QBImagePickerControllerFilterTypePhotos:{
//                NSString *format = (self.numberOfPhotos == 1) ? @"format_photo" : @"format_photos";
//                footerView.textLabel.text = [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(format,
//                                                                                                          @"QBImagePickerController",
//                                                                                                          [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"QBImagePickerController" ofType:@"bundle"]],
//                                                                                                          nil),
//                                                                                                  self.numberOfPhotos
//                                                                                                  ];
                footerView.textLabel.text = [NSString stringWithFormat:@"%ld Photo, %ld Video",
                                             (unsigned long)self.numberOfPhotos,\
                                             (unsigned long)self.numberOfVideos\
                                             ];
                break;
            }
                
            case QBImagePickerControllerFilterTypeVideos:{
//                NSString *format = (self.numberOfVideos == 1) ? @"format_video" : @"format_videos";
//                footerView.textLabel.text = [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(format,
//                                                                                                          @"QBImagePickerController",
//                                                                                                          [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"QBImagePickerController" ofType:@"bundle"]],
//                                                                                                          nil),
//                                                                                                  self.numberOfVideos
//                                                                                                  ];

                footerView.textLabel.text = [NSString stringWithFormat:@"%ld Photo, %ld Video",
                                             (unsigned long)self.numberOfPhotos,\
                                             (unsigned long)self.numberOfVideos\
                                             ];
                break;
            }
        }
        
        return footerView;
    }
    
    return nil;
}

 

https://github.com/questbeat/QBImagePicker 

转载于:https://www.cnblogs.com/nonato/p/4402652.html

 类似资料: