因为我在模仿美图秀秀的功能,在使用相册时候,UIImagePickerController本来就是一个UINavigationController的子类,所以没有办法使用push,所以做了一个自定义的非UINavigationController子类的相册。使用的api是ios8以上提供的photokit。

  一、获取相册的所有相册集

  例如:个人收藏,最近添加,相机胶卷等。

  1.使用+ (PHFetchResult<PHAssetCollection *> *)fetchAssetCollectionsWithType:(PHAssetCollectionType)type subtype:(PHAssetCollectionSubtype)subtype options:(nullable PHFetchOptions *)options方法来获取相册集集合

PHFetchResult返回了结果

  2.使用+ (PHFetchResult<PHAsset *> *)fetchAssetsInAssetCollection:(PHAssetCollection *)assetCollection options:(nullable PHFetchOptions *)options方法来获取相册集内的所有照片资源

  

- (PHFetchResult *)fetchAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending
{
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]];
PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options]; return result;
} - (NSArray <PHAsset *> *)getAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending
{
NSMutableArray <PHAsset *> *mArr = [NSMutableArray array];
PHFetchResult *result = [self fetchAssetsInAssetCollection:assetCollection ascending:ascending];
[result enumerateObjectsUsingBlock:^(PHAsset * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.mediaType == PHAssetMediaTypeImage) {
[mArr addObject:obj];
}
}]; return mArr;
} - (NSArray<PHAsset *> *)getAllAssetInPhotoAblumWithAscending:(BOOL)ascending
{
NSMutableArray<PHAsset *> *assets = [NSMutableArray array]; PHFetchOptions *option = [[PHFetchOptions alloc] init]; option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]]; PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:option]; [result enumerateObjectsUsingBlock:^(PHAsset * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[assets addObject:obj];
}]; return assets;
} - (NSArray <FWPhotoAlbums *> *)getPhotoAlbums
{
NSMutableArray<FWPhotoAlbums *> *mArr = [NSMutableArray array];
PHFetchResult *smartAlbum = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
[smartAlbum enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.assetCollectionSubtype != && obj.assetCollectionSubtype < ) {
NSArray <PHAsset *> *assets = [self getAssetsInAssetCollection:obj ascending:NO];
if ([assets count]) {
FWPhotoAlbums *pa = [[FWPhotoAlbums alloc] init];
pa.albumName = [self TitleOfAlbumForChinse:obj.localizedTitle];
pa.albumImageCount = [assets count];
pa.firstImageAsset = assets.firstObject;
pa.assetCollection = obj;
[mArr addObject:pa];
}
}
}]; PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
NSArray<PHAsset *> *assets = [self getAssetsInAssetCollection:collection ascending:YES];
if (assets.count > ) {
FWPhotoAlbums *pa = [[FWPhotoAlbums alloc] init];
pa.albumName = collection.localizedTitle;
pa.albumImageCount = [assets count];
pa.firstImageAsset = assets.firstObject;
pa.assetCollection = collection;
[mArr addObject:pa];
}
}]; return mArr;
}

由于系统返回的相册集名称为英文,我们需要转换为中文

- (NSString *)TitleOfAlbumForChinse:(NSString *)title
{
if ([title isEqualToString:@"Slo-mo"]) {
return @"慢动作";
} else if ([title isEqualToString:@"Recently Added"]) {
return @"最近添加";
} else if ([title isEqualToString:@"Favorites"]) {
return @"个人收藏";
} else if ([title isEqualToString:@"Recently Deleted"]) {
return @"最近删除";
} else if ([title isEqualToString:@"Videos"]) {
return @"视频";
} else if ([title isEqualToString:@"All Photos"]) {
return @"所有照片";
} else if ([title isEqualToString:@"Selfies"]) {
return @"自拍";
} else if ([title isEqualToString:@"Screenshots"]) {
return @"屏幕快照";
} else if ([title isEqualToString:@"Camera Roll"]) {
return @"相机胶卷";
}
return nil;
}

二、获取照片

- (void)requestImageForAsset:(PHAsset *)asset size:(CGSize)size resizeMode:(PHImageRequestOptionsResizeMode)resizeMode completion:(void (^)(UIImage *, NSDictionary *))completion
{
static PHImageRequestID requestID = -;
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat width = MIN(WIDTH, );
if (requestID >= && size.width/width==scale) {
[[PHCachingImageManager defaultManager] cancelImageRequest:requestID];
} PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.resizeMode = resizeMode;
option.networkAccessAllowed = YES; requestID = [[PHCachingImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFill options:option resultHandler:^(UIImage * _Nullable image, NSDictionary * _Nullable info) {
BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey];
if (downloadFinined && completion) {
completion(image, info);
}
}];
}

三、拍照

  [_mCamera capturePhotoAsJPEGProcessedUpToFilter:_mFilter withCompletionHandler:^(NSData *processedJPEG, NSError *error){
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:processedJPEG options:nil];
} completionHandler:^(BOOL success, NSError * _Nullable error) { }];
}];

效果

       

代码

1.模型类,存储相册集名称,相片数,第一张照片以及该相册集包含的所有照片集合

#import <Foundation/Foundation.h>
#import <photos/photos.h> @interface FWPhotoAlbums : NSObject @property (nonatomic, copy) NSString *albumName; //相册名字
@property (nonatomic, assign) NSUInteger albumImageCount; //该相册内相片数量
@property (nonatomic, strong) PHAsset *firstImageAsset; //相册第一张图片缩略图
@property (nonatomic, strong) PHAssetCollection *assetCollection; //相册集,通过该属性获取该相册集下所有照片 @end

FWPhotoAlbums.h

#import "FWPhotoAlbums.h"

@implementation FWPhotoAlbums

@end

FWPhotoAlbums.m

2.照片管理类,负责获取资源集合,请求照片

#import <Foundation/Foundation.h>
#import "FWPhotoAlbums.h" @interface FWPhotoManager : NSObject + (instancetype)sharedManager; - (NSArray <FWPhotoAlbums *> *)getPhotoAlbums; - (NSArray<PHAsset *> *)getAllAssetInPhotoAblumWithAscending:(BOOL)ascending; - (NSArray<PHAsset *> *)getAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending; - (void)requestImageForAsset:(PHAsset *)asset size:(CGSize)size resizeMode:(PHImageRequestOptionsResizeMode)resizeMode completion:(void (^)(UIImage *image, NSDictionary *info))completion; - (void)requestImageForAsset:(PHAsset *)asset scale:(CGFloat)scale resizeMode:(PHImageRequestOptionsResizeMode)resizeMode completion:(void (^)(UIImage *image))completion; - (void)getPhotosBytesWithArray:(NSArray *)photos completion:(void (^)(NSString *photosBytes))completion; - (BOOL)judgeAssetisInLocalAblum:(PHAsset *)asset ;
@end

FWPhotoManager.h

#import "FWPhotoManager.h"

@implementation FWPhotoManager

+ (instancetype)sharedManager
{
static FWPhotoManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[super allocWithZone:NULL] init];
}); return manager;
} + (instancetype)allocWithZone:(struct _NSZone *)zone
{
return [self sharedManager];
} - (PHFetchResult *)fetchAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending
{
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]];
PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options]; return result;
} - (NSArray <PHAsset *> *)getAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending
{
NSMutableArray <PHAsset *> *mArr = [NSMutableArray array];
PHFetchResult *result = [self fetchAssetsInAssetCollection:assetCollection ascending:ascending];
[result enumerateObjectsUsingBlock:^(PHAsset * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.mediaType == PHAssetMediaTypeImage) {
[mArr addObject:obj];
}
}]; return mArr;
} - (NSArray<PHAsset *> *)getAllAssetInPhotoAblumWithAscending:(BOOL)ascending
{
NSMutableArray<PHAsset *> *assets = [NSMutableArray array]; PHFetchOptions *option = [[PHFetchOptions alloc] init]; option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]]; PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:option]; [result enumerateObjectsUsingBlock:^(PHAsset * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[assets addObject:obj];
}]; return assets;
} - (NSArray <FWPhotoAlbums *> *)getPhotoAlbums
{
NSMutableArray<FWPhotoAlbums *> *mArr = [NSMutableArray array];
PHFetchResult *smartAlbum = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
[smartAlbum enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.assetCollectionSubtype != && obj.assetCollectionSubtype < ) {
NSArray <PHAsset *> *assets = [self getAssetsInAssetCollection:obj ascending:NO];
if ([assets count]) {
FWPhotoAlbums *pa = [[FWPhotoAlbums alloc] init];
pa.albumName = [self TitleOfAlbumForChinse:obj.localizedTitle];
pa.albumImageCount = [assets count];
pa.firstImageAsset = assets.firstObject;
pa.assetCollection = obj;
[mArr addObject:pa];
}
}
}]; PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
NSArray<PHAsset *> *assets = [self getAssetsInAssetCollection:collection ascending:YES];
if (assets.count > ) {
FWPhotoAlbums *pa = [[FWPhotoAlbums alloc] init];
pa.albumName = collection.localizedTitle;
pa.albumImageCount = [assets count];
pa.firstImageAsset = assets.firstObject;
pa.assetCollection = collection;
[mArr addObject:pa];
}
}]; return mArr;
} - (NSString *)TitleOfAlbumForChinse:(NSString *)title
{
if ([title isEqualToString:@"Slo-mo"]) {
return @"慢动作";
} else if ([title isEqualToString:@"Recently Added"]) {
return @"最近添加";
} else if ([title isEqualToString:@"Favorites"]) {
return @"个人收藏";
} else if ([title isEqualToString:@"Recently Deleted"]) {
return @"最近删除";
} else if ([title isEqualToString:@"Videos"]) {
return @"视频";
} else if ([title isEqualToString:@"All Photos"]) {
return @"所有照片";
} else if ([title isEqualToString:@"Selfies"]) {
return @"自拍";
} else if ([title isEqualToString:@"Screenshots"]) {
return @"屏幕快照";
} else if ([title isEqualToString:@"Camera Roll"]) {
return @"相机胶卷";
}
return nil;
} - (void)requestImageForAsset:(PHAsset *)asset size:(CGSize)size resizeMode:(PHImageRequestOptionsResizeMode)resizeMode completion:(void (^)(UIImage *, NSDictionary *))completion
{
static PHImageRequestID requestID = -;
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat width = MIN(WIDTH, );
if (requestID >= && size.width/width==scale) {
[[PHCachingImageManager defaultManager] cancelImageRequest:requestID];
} PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.resizeMode = resizeMode;
option.networkAccessAllowed = YES; requestID = [[PHCachingImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFill options:option resultHandler:^(UIImage * _Nullable image, NSDictionary * _Nullable info) {
BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey];
if (downloadFinined && completion) {
completion(image, info);
}
}];
} - (void)requestImageForAsset:(PHAsset *)asset scale:(CGFloat)scale resizeMode:(PHImageRequestOptionsResizeMode)resizeMode completion:(void (^)(UIImage *image))completion
{
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.resizeMode = resizeMode;//控制照片尺寸
option.networkAccessAllowed = YES; [[PHCachingImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue];
if (downloadFinined && completion) {
CGFloat sca = imageData.length/(CGFloat)UIImageJPEGRepresentation([UIImage imageWithData:imageData], ).length;
NSData *data = UIImageJPEGRepresentation([UIImage imageWithData:imageData], scale==?sca:sca/);
completion([UIImage imageWithData:data]);
}
}];
} - (BOOL)judgeAssetisInLocalAblum:(PHAsset *)asset
{
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.networkAccessAllowed = NO;
option.synchronous = YES; __block BOOL isInLocalAblum = YES; [[PHCachingImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
isInLocalAblum = imageData ? YES : NO;
}];
return isInLocalAblum;
} @end

FWPhotoManager.m

3.显示相册FWPhotoAlbumTableViewController

#import <UIKit/UIKit.h>
#import <Photos/Photos.h> @interface FWPhotoAlbumTableViewController : UITableViewController
//最大选择数
@property (nonatomic, assign) NSInteger maxSelectCount;
//是否选择了原图
@property (nonatomic, assign) BOOL isCanSelectMorePhotos;
//当前已经选择的图片
//@property (nonatomic, strong) NSMutableArray<ZLSelectPhotoModel *> *arraySelectPhotos; //选则完成后回调
//@property (nonatomic, copy) void (^DoneBlock)(NSArray<ZLSelectPhotoModel *> *selPhotoModels, BOOL isSelectOriginalPhoto);
//取消选择后回调
@property (nonatomic, copy) void (^CancelBlock)(); @end

FWPhotoAlbumTableViewController.h

#import "FWPhotoAlbumTableViewController.h"
#import "FWPhotoManager.h"
#import "UIButton+TextAndImageHorizontalDisplay.h"
#import "FWPhotoCollectionViewController.h"
#import "FWPhotosLayout.h" @interface FWPhotoAlbumTableViewController ()
{
NSMutableArray<FWPhotoAlbums *> *mArr;
} @end @implementation FWPhotoAlbumTableViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"相册"; mArr= [[[FWPhotoManager sharedManager] getPhotoAlbums] mutableCopy]; [self initNavgationBar]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
} - (void)initNavgationBar
{
UIButton *rightBar = [UIButton buttonWithType:UIButtonTypeSystem];
rightBar.frame = CGRectMake(, , , );
[rightBar setImage:[UIImage imageNamed:@"Camera"] withTitle:@"相机" forState:UIControlStateNormal];
[rightBar addTarget:self action:@selector(cameraClicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBar];
} - (void)cameraClicked
{ } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [mArr count];
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
FWPhotoAlbums *album = mArr[indexPath.row];
cell.textLabel.text =album.albumName;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d张",album.albumImageCount];
cell.detailTextLabel.textColor = [UIColor grayColor];
__block UIImage *img = nil;
[[FWPhotoManager sharedManager] requestImageForAsset:album.firstImageAsset size:CGSizeMake( * , * ) resizeMode:PHImageRequestOptionsResizeModeExact completion:^(UIImage *image, NSDictionary *info) {
img = image;
}];
cell.imageView.image = img;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
return cell;
} - (BOOL)prefersStatusBarHidden
{
return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FWPhotoAlbums *model = [mArr objectAtIndex:indexPath.row];
FWPhotosLayout *layout = [[FWPhotosLayout alloc] init];
layout.minimumInteritemSpacing = 1.5;
layout.minimumLineSpacing = 5.0;
FWPhotoCollectionViewController *vc = [[FWPhotoCollectionViewController alloc] initWithCollectionViewLayout:layout model:model];
[self.navigationController pushViewController:vc animated:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

FWPhotoAlbumTableViewController.m

4.显示相册集内所有照片FWPhotoCollectionViewController

#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
#import "FWPhotoAlbums.h" @interface FWPhotoCollectionViewController : UICollectionViewController @property (nonatomic, strong) FWPhotoAlbums *model; - (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout model:(FWPhotoAlbums *)model; @end

FWPhotoCollectionViewController.h

//
// FWPhotoCollectionViewController.m
// FWLifeApp
//
// Created by Forrest Woo on 16/9/23.
// Copyright © 2016年 ForrstWoo. All rights reserved.
// #import "FWPhotoCollectionViewController.h"
#import "FWPhotoCell.h"
#import "FWPhotoManager.h"
#import "FWPhotosLayout.h"
#import "FWDisplayBigImageViewController.h" @interface FWPhotoCollectionViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
{
NSArray<PHAsset *> *_dataSouce;
}
@property (nonatomic, strong) PHAssetCollection *assetCollection;
@end @implementation FWPhotoCollectionViewController static NSString * const reuseIdentifier = @"Cell"; - (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout model:(FWPhotoAlbums *)model
{
if (self = [super initWithCollectionViewLayout:layout]) {
self.model = model;
} return self;
} - (void)viewDidLoad {
[super viewDidLoad]; // Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;
// self.navigationController.navigationBarHidden = YES;
// Register cell classes
CGRect frame = self.collectionView.frame;
frame.origin.y+=;
self.collectionView.frame = frame;
self.view.backgroundColor = [UIColor whiteColor]; [self initSource];
} //- (void)setModel:(FWPhotoAlbums *)model
//{
// self.model = model;
//
// [self initSource];
//} - (BOOL)prefersStatusBarHidden
{
return YES;
}
- (void)initSource
{
[self.collectionView registerClass:[FWPhotoCell class] forCellWithReuseIdentifier:reuseIdentifier];
self.title = self.model.albumName;
self.automaticallyAdjustsScrollViewInsets = NO;
// Do any additional setup after loading the view.
self.assetCollection = self.model.assetCollection;
_dataSouce = [[FWPhotoManager sharedManager] getAssetsInAssetCollection:self.assetCollection ascending:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark <UICollectionViewDataSource> - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return ;
} - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [_dataSouce count];
} - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
FWPhotosLayout *lay = (FWPhotosLayout *)collectionViewLayout;
return CGSizeMake([lay cellWidth],[lay cellWidth]);
} - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FWPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
PHAsset *asset = _dataSouce[indexPath.row];
__block UIImage *bImage = nil;
CGSize size = cell.frame.size;
size.width *= ;
size.height *= ;
[[FWPhotoManager sharedManager] requestImageForAsset:asset size:size resizeMode:PHImageRequestOptionsResizeModeFast completion:^(UIImage *image, NSDictionary *info) {
bImage = image;
}]; [cell setImage:bImage]; return cell;
} - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
PHAsset *asset = _dataSouce[indexPath.row]; [[FWPhotoManager sharedManager] requestImageForAsset:asset size:PHImageManagerMaximumSize resizeMode:PHImageRequestOptionsResizeModeFast completion:^(UIImage *image, NSDictionary *info) {
FWDisplayBigImageViewController *vc = [[FWDisplayBigImageViewController alloc] initWithImage:image];
[self.navigationController pushViewController:vc animated:YES];
}]; }
#pragma mark <UICollectionViewDelegate> @end

FWPhotoCollectionViewController.m

5.布局类

#import <UIKit/UIKit.h>

@interface FWPhotosLayout : UICollectionViewFlowLayout

@property (nonatomic, assign,readonly)CGFloat cellWidth;

@end

FWPhotosLayout.h

#import "FWPhotosLayout.h"

@interface FWPhotosLayout ()
@property NSInteger countOfRow;
@end @implementation FWPhotosLayout - (void)prepareLayout
{
[super prepareLayout]; self.countOfRow = ceilf([self.collectionView numberOfItemsInSection:] / 4.0);
} - (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes *attris = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
NSInteger currentRow = indexPath.item / ; CGRect frame = CGRectMake( (indexPath.item % ) * ([self cellWidth] + ),currentRow * ([self cellWidth] + ), [self cellWidth], [self cellWidth]);
attris.frame = frame;
attris.zIndex = ; return attris;
} - (CGFloat)cellWidth
{
return (WIDTH - * ) / ;
} - (CGSize)collectionViewContentSize
{
return CGSizeMake(WIDTH, self.countOfRow * ([self cellWidth] + ) + );
}
@end

FWPhotosLayout.m

代码下载

通过自定义相册来介绍photo library的使用的更多相关文章

  1. iOS--app自定义相册--创建相簿,存储图片到手机

    我们在APP中点击照片,都会显示出大图,然后在大图的上面会有个保存照片的按钮,照片直接保存到了系统的相册中,但是因为公司产品的需要,让你创建和APP同名的相册保存在里面,那么就对了,可以看下具体的代码 ...

  2. 借助TZImagePickerController三方库理解自定义相册

    借助TZImagePickerController三方库理解自定义相册 1.整体架构分析 整体框架大致可以分为几个部分 <1>工具类-TZImageManager:这个类主要是工作是提供一 ...

  3. iOS分段选择器、旅行App、标度尺、对对碰小游戏、自定义相册等源码

    iOS精选源码 企业级开源项目,模仿艺龙旅行App 标签选择器--LeeTagView CSSegmentedControl常用的分段选择器,简单易用! 仿微信左滑删除 IOS左滑返回 输入框 iOS ...

  4. iOS中怎么存储照片到自定义相册

    在市场上主流App中,大多数App都具有存储图片到自己App的相册中.苹果提供的方法只能存储图片到系统相册,下面讲一下怎么实现: 实现思路:  1.对系统相册进行操作的前提必须导入#import &l ...

  5. android自定义相册 支持低端机不内存溢出

    1 之前在网上看的自定义相册很多时候在低端机都会内存溢出开始上代码把 首先我们要拿到图片的所有路径 cursor = context.getContentResolver().query( Media ...

  6. Interaction with the camera or the photo library

    As we said before, we need a delegate to deal with the user interaction with the camera or the photo ...

  7. 非自定义和自定义Dialog的介绍!!!

    一.非自定义Dialog的几种形式介绍 转自:http://www.kwstu.com/ArticleView/kwstu_20139682354515 前言 对话框对于应用也是必不可少的一个组件,在 ...

  8. ios---photo实现保存图片到自定义相册

    #import "XMGSeeBigPictureViewController.h" #import "XMGTopic.h" #import <SVPr ...

  9. uniapp+nvue实现仿微信/得物相册插件:选择界面 +自定义相册+图片视频过滤

    本篇文章基于uniapp 框架+ nvue,实现了uniapp仿微信/得物相册选择功能实例项目,该插件实例实现了以下功能: 1: 相册过滤 2: 图视频过滤 3: 界面UI定制化 4: 栅格列数定制化 ...

随机推荐

  1. Java-BlockingQueue的使用

    每次都是隔很长时间才在博客中写点什么,说自己忙吧,这是给自己的一个借口,其实呢还是懒啊.哎... 最近项目中有个对比的需求,需要从日志文件中获取到参数,然后调用不同的API,进行结果的对比.但是不知用 ...

  2. Centos6.6下安装MariaDB步骤,利用yum进行安装

    1.在/etc/yum.repos.d/下建立MariaDB.repo文件 可以在Win下编辑好此文件,然后通过SSH远程复制过去. 2.MariaDB.repo内容要根据MariaDB官方提供的re ...

  3. 【C语言学习】《C Primer Plus》第6章 C控制语句:循环

    学习总结 1.循环的语法跟其他语言的没差多少,可能大多数语言都在C的基础上发展出来的,所以大同小异不奇怪. 2.在判断表达式里,C语言只有0被认为是假,所有非零值正整数都被认为真. #include ...

  4. 将整数转换成二进制的java小程序

    首先我们知道,将整数转换成二进制是将整数除二取余将最后除得的数和得到的余数从下向上写,组成得到的二进制数. java程序实现如下: public class ChangeToErjinzhi { pu ...

  5. bitmap算法

    概述 所谓bitmap就是用一个bit位来标记某个元素对应的value,而key即是这个元素.由于采用bit为单位来存储数据,因此在可以大大的节省存储空间 算法思想 32位机器上,一个整形,比如int ...

  6. 一个线上运营着3000+人的游戏,因为我不小心一个DROP DATABASE,全没了。 怎么办??跟我HOLD住!!!

    前言 今天下午3点,我按照惯例,打开游戏服务器,开新服部署嘛,游戏在腾讯开放平台,简单.闭着眼睛都OK.于是一轮子的复制黏贴拷贝,把服务器加起来,然后启动查看日志. ....突然发现不断的有Excep ...

  7. CocoaPods 深入使用

    在 CocoaPods 使用中介绍了基本的使用 写项目的时候想用到 SQLite.swift第三方库,但是问题来了 pod search SQLite.swift  //执行这条语句,搜索不到结果 但 ...

  8. 利用定时器实时显示<input type="range"/>的值

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 每天一个linux命令(51):lsof命令

    lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以如传输控制协议 ...

  10. WebBrowser设置Cookie

    在winform里面经常会用到WebBrowser,这是一个难点就是如何设置cookies,注意,Docment对象是只读的,所以WebBrowser.Docment.cookie也就只有get方法, ...