Demo,復雜完成了照片選擇器的功用
PhotoKitDemo
歡送給Star!!!
提供檢索或生成預覽縮略圖以及與Photos資源相關聯的全尺寸圖片或視頻數據的辦法,這些圖片或視頻數據針對批量預加載少量資產停止了優化。
為了在運用許多資產時疾速運轉,緩存圖像管理器可以在後台預備資產圖像,以便在當前懇求單個圖像時消弭延遲。例如,當您希望運用照片或視頻資源的縮略圖填充集合視圖或相似的用戶界面時,請運用緩存圖片管理器。
PHCachingImageManager類的許多關鍵功用由其超類PHImageManager定義。有關詳細信息,請參閱PHImageManager。
要運用緩存圖像管理器:
從照片提取辦法前往的資源或集合的有序列表。
當您在PHAsset,PHCollection,PHAssetCollection和PHCollectionList類中運用類辦法來檢索對象時,Photos會在提取後果中提供生成的對象。您可以運用與NSArray類所運用的辦法和商定相反的方式訪問獲取後果的內容。但是,與NSArray對象不同,PHFetchResult對象依據需求從Photos庫靜態加載其內容,即便在處置少量後果時也能提供最佳功能。
提取後果提供對其內容的線程平安訪問。抓取後,抓取後果的計數值為常量,並且抓取後果中的一切對象堅持相反的localIdentifier值。 (要獲取更新的內容以獲取,請運用共享的PHPhotoLibrary對象注冊變卦察看器。)
提取後果緩存其內容,在最近訪問的索引四周保存一批對象。由於批處置之外的對象不再緩存,訪問這些對象會招致重新獲取這些對象。此進程能夠會招致更改以前從這些對象讀取的值。
一組影響過濾,排序和管理後果的選項,當您提取資源或集合對象時,Photos會前往。
在PHAsset,PHCollection,PHAssetCollection和PHCollectionList類上運用類辦法來獲取資產或集合時,會生成包括所懇求對象的PHFetchResult對象。 您指定的選項控制抓取後果的對象,包括這些對象在抓取後果中的陳列方式,以及Google相冊應如何告訴您的使用抓取後果的更改。
照片僅支持謂詞和sortDescriptors屬性的一組受限制的鍵。 可用鍵的集合取決於您用來獲取資源或集合的類。
累了…
NSString *kXJYGridCollectionViewCell = @"XJYGridCollectionViewCell";
@interface XJYPhotoSelectorController ()<PHPhotoLibraryChangeObserver>
@property (nonatomic, strong) XJYCollectionViewFlowLayout *layout;
//抓取後果
@property (nonatomic, strong) PHFetchResult<PHAsset *> *fetchResult;
@property (nonatomic, strong) PHCachingImageManager *imageManager;
@property (nonatomic, strong) PHAssetCollection *assetCollection;
@property (nonatomic, assign) CGSize thumbnailSize;
@property (nonatomic, assign) CGRect previousPreheatRect;
@end
@implementation XJYPhotoSelectorController
#pragma mark Class Method
- (instancetype)init {
self = [self initWithCollectionViewLayout:self.layout];
return self;
}
- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout {
if (self = [super initWithCollectionViewLayout:self.layout]) {
}
return self;
}
#pragma mark View Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.previousPreheatRect = CGRectZero;
[self resetCachedAssets];
self.automaticallyAdjustsScrollVieWinsets = YES;
self.collectionView.backgroundColor = [UIColor whiteColor];
#pragma mark PhotoKit
self.imageManager = [PHCachingImageManager defaultManager];
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
self.fetchResult = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
#pragma mark Register Cell
// Register cell classes
[self.collectionView registerClass:[XJYGridCollectionViewCell class] forCellWithReuseIdentifier:kXJYGridCollectionViewCell];
//Scroll to End
NSInteger section = [self.collectionView numberOfSections] - 1;
NSInteger item = [self.collectionView numberOfItemsInSection:section] - 1;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:section];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionBottom) animated:YES];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self updateCachedAssets];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#define kScreen_Height [UIScreen mainScreen].bounds.size.height
#define kScreen_Width [UIScreen mainScreen].bounds.size.width
#pragma mark Getter
- (XJYCollectionViewFlowLayout *)layout {
if (!_layout) {
_layout = [[XJYCollectionViewFlowLayout alloc] init];
_layout.itemSize = CGSizeMake((kScreen_Width-20)/4, (kScreen_Width-20)/4);
_layout.minimumLineSpacing = 2.0;
_layout.minimumInteritemSpacing = 2.0;
_layout.scrollDirection = UICollectionViewScrollDirectionVertical;
_layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
}
return _layout;
}
- (CGSize)thumbnailSize {
CGFloat scale = [UIScreen mainScreen].scale;
_thumbnailSize = CGSizeMake(scale * self.layout.itemSize.width, scale * self.layout.itemSize.height);
return _thumbnailSize;
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.fetchResult.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XJYGridCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kXJYGridCollectionViewCell forIndexPath:indexPath];
//依據Index 獲取asset
PHAsset *asset = [self.fetchResult objectAtIndex:indexPath.item];
//設置cell representedAssetIdentifier
cell.representedAssetIdentifier = asset.localIdentifier;
//imageManager 懇求image
[self.imageManager requestImageForAsset:asset targetSize:self.thumbnailSize contentMode:PHImageContentModeaspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
if ([cell.representedAssetIdentifier isEqualToString: asset.localIdentifier]) {
cell.image = result;
}
}];
return cell;
}
【iOS PhotoKit 教程】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!