本期部分项目需求:获取相簿特定相册的图片,展示出来
手机系统版本判断宏
define iOS(version) (([[[UIDevice currentDevice] systemVersion] intValue] >= version)?1:0)
static NSString * kGroupName = @"XYZ";//工程创建的XYZ相册文件夹名称
首先判断是否存在工程创建的XYZ相册文件夹,例如qq,微博
//存在返回YES,不存在返回NO
__block BOOL haveXYZGroup = NO;
- __block dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
- dispatch_queue_t queue = dispatch_queue_create("groupBlock", NULL);`
if (iOS(8)) {
dispatch_async(queue, ^{
//获取相册集合
PHFetchResult * fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
//set up fetch options, mediaType is image.
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage];
for (NSInteger i = 0; i < fetchResult.count; i++) {
PHAssetCollection *assetCollection = fetchResult[i];
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];
NSLog(@"grouName - %@, count - %@", assetCollection.localizedTitle, @(assetsFetchResult.count));
NSLog(@"assetCollection.localizedTitle - %@", assetCollection.localizedTitle);
if ([assetCollection.localizedTitle isEqualToString:kGroupName]) {
haveXYZGroup = YES;
break;
}
}
dispatch_semaphore_signal(semaphore);
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}else{
dispatch_async(queue, ^{
NSMutableArray *groupMarr = [NSMutableArray arrayWithCapacity:0];
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group){
[groupMarr addObject:group];
}
else{
for (ALAssetsGroup *group in groupMarr){
NSString *groupName = [group valueForProperty:ALAssetsGroupPropertyName];
if ([groupName isEqualToString:kGroupName]){
haveXYZGroup = YES;
break;
}
}
dispatch_semaphore_signal(semaphore);
}
} failureBlock:^(NSError *error) {
self.title = @"访问照片失败"; // Photo-access is disabled.
[self showAlertViewTitle:nil msg:@"请在设置->隐私->照片中,确认"XYZ"处于打开状态" cancellTtile:@"好" sureTitle:nil];
}];
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
return haveXYZGroup;
}
- (void)getXYZGroupPitures{
/加载菊花视图
if ([self isExistWithGroupName:kGroupName] == YES) {
if (iOS(8)) {
[self checkAllAlbumGroupiOS8];
}else{
[self checkAllAlbumGroup];
}
}else{
/去掉菊花视图
NSString * tipMsg = @"图片库还没有建立,先去拍摄一张图片吧。";
[UIToastView showToastViewWithContent:tipMsg andRect:CGRectMake(40, 100, SCREEN_WIDTH - 40*2, 40) andTime:1.5f andObject:self];
}
}
- (void)checkAllAlbumGroup{
if (!self.assetsLibrary) {
self.assetsLibrary = [[ALAssetsLibrary alloc]init];
}
dispatch_async(dispatch_get_main_queue(), ^{
/ Notice : ALAssetsGroupAll doesn't include ALAssetsGroupLibrary.
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
NSString * groupName = (NSString *)[group valueForProperty:ALAssetsGroupPropertyName];
if ([groupName isEqualToString:kGroupName]) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (group.numberOfAssets > 0) {
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
NSString * assetType = [result valueForProperty:ALAssetPropertyType];
if([assetType isEqualToString:ALAssetTypePhoto]){
/得到单个ALAsset对象,简单数据处理,装入容器EBAssetModel为自定义模型,最后备注
EBAssetModel * assetModel = [[EBAssetModel alloc]init];
assetModel.asset = result;
}
}
if (index == 0) {
//处理图片数据,排序
[self performSelectorOnMainThread:@selector(finishGetAllPhotos) withObject:nil waitUntilDone:YES];
}
}];
}else{
/去掉菊花视图
NSString * tipMsg = @"图片库暂无图片,先去拍摄一张图片吧。";
[UIToastView showToastViewWithContent:tipMsg andRect:CGRectMake(40, 100, SCREEN_WIDTH - 40*2, 40) andTime:1.5f andObject:self];
}
}
}
} failureBlock:^(NSError *error) {
/Group not found!
self.title = @"访问照片失败"; // Photo-access is disabled.
[self showAlertViewTitle:nil msg:@"请在设置->隐私->照片中,确认搜房帮处于打开状态" cancellTtile:@"好" sureTitle:nil];
}];
});
}
- (void)checkAllAlbumGroupiOS8{
//获取相册集合
PHFetchResult * fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
//set up fetch options, mediaType is image.
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage];
for (NSInteger i = 0; i < fetchResult.count; i++) {
PHAssetCollection *assetCollection = fetchResult[i];
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];
if ([assetCollection.localizedTitle isEqualToString:kGroupName]) {
if (assetsFetchResult.count > 0) {
[assetsFetchResult enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
//you have got your image type asset.
if (obj) {
/得到单个PHAsset对象,简单数据处理,装入容器
EBAssetModel * assetModel = [[EBAssetModel alloc]init];
assetModel.assetPH = (PHAsset *)obj;
}
if (idx == 0) {
//处理图片数据,排序
[self performSelectorOnMainThread:@selector(finishGetAllPhotos) withObject:nil waitUntilDone:YES];
}
}];
}
else{
/去掉菊花视图
NSString * tipMsg = @"图片库暂无图片,先去拍摄一张图片吧。";
[UIToastView showToastViewWithContent:tipMsg andRect:CGRectMake(40, 100, SCREEN_WIDTH - 40*2, 40) andTime:1.5f andObject:self];
}
}
}
}
- (void)finishGetAllPhotos
{
//获取数据成功,数据加工成可以满足项目需求为止,然后创建视图,刷新即可
}
注意点:
容器中装的是EBAssetModel的model
导入2个库AssetsLibrary/AssetsLibrary.h>和Photos/Photos.h>
@interface EBAssetModel : NSObject
@property (nonatomic, strong) ALAsset * asset;
@property (nonatomic, strong) PHAsset * assetPH;
@end
首先获取单个对象模型然后获取图片
EBAssetModel * assetModel = ...
1.缩略图
__block UIImage * image = nil;
if (iOS(8)) {
CGFloat kThumbnailW = kW_PICTURE;
CGFloat kThumbnailH = kH_PICTURE;
PHImageRequestOptions * requestOptions = [[PHImageRequestOptions alloc]init];
requestOptions.synchronous = YES;
requestOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
[[PHImageManager defaultManager] requestImageForAsset:assetModel.assetPH
targetSize:CGSizeMake(kThumbnailW, kThumbnailH)
contentMode:PHImageContentModeAspectFill
options:requestOptions
resultHandler:^(UIImage *result, NSDictionary *info) {
image = result;
}];
}else{
image = [UIImage imageWithCGImage:[assetModel.asset thumbnail]];
}
if (iOS(8)) {
[self.view showActivityViewAtCenter];
PHImageRequestOptions * requestOptions = [[PHImageRequestOptions alloc]init];
requestOptions.synchronous = YES;
requestOptions.networkAccessAllowed = YES;
requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
[[PHImageManager defaultManager] requestImageForAsset:assetModel.assetPH
targetSize:PHImageManagerMaximumSize
contentMode:PHImageContentModeAspectFill
options:requestOptions
resultHandler:^(UIImage *result, NSDictionary *info) {
imageOriginal = result;
}];
}else{
imageOriginal = [UIImage imageWithCGImage:[assetModel.asset.defaultRepresentation fullScreenImage]];
}
手机系统版本判断宏
define iOS(version) (([[[UIDevice currentDevice] systemVersion] intValue] >= version)?1:0)
static NSString * kGroupName = @"XYZ";//工程创建的XYZ相册文件夹名称
首先判断是否存在工程创建的XYZ相册文件夹,例如qq,微博
//存在返回YES,不存在返回NO
__block BOOL haveXYZGroup = NO;
- __block dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
- dispatch_queue_t queue = dispatch_queue_create("groupBlock", NULL);`
if (iOS(8)) {
dispatch_async(queue, ^{
//获取相册集合
PHFetchResult * fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
//set up fetch options, mediaType is image.
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage];
for (NSInteger i = 0; i < fetchResult.count; i++) {
PHAssetCollection *assetCollection = fetchResult[i];
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];
NSLog(@"grouName - %@, count - %@", assetCollection.localizedTitle, @(assetsFetchResult.count));
NSLog(@"assetCollection.localizedTitle - %@", assetCollection.localizedTitle);
if ([assetCollection.localizedTitle isEqualToString:kGroupName]) {
haveXYZGroup = YES;
break;
}
}
dispatch_semaphore_signal(semaphore);
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}else{
dispatch_async(queue, ^{
NSMutableArray *groupMarr = [NSMutableArray arrayWithCapacity:0];
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group){
[groupMarr addObject:group];
}
else{
for (ALAssetsGroup *group in groupMarr){
NSString *groupName = [group valueForProperty:ALAssetsGroupPropertyName];
if ([groupName isEqualToString:kGroupName]){
haveXYZGroup = YES;
break;
}
}
dispatch_semaphore_signal(semaphore);
}
} failureBlock:^(NSError *error) {
self.title = @"访问照片失败"; // Photo-access is disabled.
[self showAlertViewTitle:nil msg:@"请在设置->隐私->照片中,确认"XYZ"处于打开状态" cancellTtile:@"好" sureTitle:nil];
}];
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
return haveXYZGroup;
}
- (void)getXYZGroupPitures{
/加载菊花视图
if ([self isExistWithGroupName:kGroupName] == YES) {
if (iOS(8)) {
[self checkAllAlbumGroupiOS8];
}else{
[self checkAllAlbumGroup];
}
}else{
/去掉菊花视图
NSString * tipMsg = @"图片库还没有建立,先去拍摄一张图片吧。";
[UIToastView showToastViewWithContent:tipMsg andRect:CGRectMake(40, 100, SCREEN_WIDTH - 40*2, 40) andTime:1.5f andObject:self];
}
}
- (void)checkAllAlbumGroup{
if (!self.assetsLibrary) {
self.assetsLibrary = [[ALAssetsLibrary alloc]init];
}
dispatch_async(dispatch_get_main_queue(), ^{
/ Notice : ALAssetsGroupAll doesn't include ALAssetsGroupLibrary.
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
NSString * groupName = (NSString *)[group valueForProperty:ALAssetsGroupPropertyName];
if ([groupName isEqualToString:kGroupName]) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (group.numberOfAssets > 0) {
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
NSString * assetType = [result valueForProperty:ALAssetPropertyType];
if([assetType isEqualToString:ALAssetTypePhoto]){
/得到单个ALAsset对象,简单数据处理,装入容器EBAssetModel为自定义模型,最后备注
EBAssetModel * assetModel = [[EBAssetModel alloc]init];
assetModel.asset = result;
}
}
if (index == 0) {
//处理图片数据,排序
[self performSelectorOnMainThread:@selector(finishGetAllPhotos) withObject:nil waitUntilDone:YES];
}
}];
}else{
/去掉菊花视图
NSString * tipMsg = @"图片库暂无图片,先去拍摄一张图片吧。";
[UIToastView showToastViewWithContent:tipMsg andRect:CGRectMake(40, 100, SCREEN_WIDTH - 40*2, 40) andTime:1.5f andObject:self];
}
}
}
} failureBlock:^(NSError *error) {
/Group not found!
self.title = @"访问照片失败"; // Photo-access is disabled.
[self showAlertViewTitle:nil msg:@"请在设置->隐私->照片中,确认搜房帮处于打开状态" cancellTtile:@"好" sureTitle:nil];
}];
});
}
- (void)checkAllAlbumGroupiOS8{
//获取相册集合
PHFetchResult * fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
//set up fetch options, mediaType is image.
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage];
for (NSInteger i = 0; i < fetchResult.count; i++) {
PHAssetCollection *assetCollection = fetchResult[i];
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];
if ([assetCollection.localizedTitle isEqualToString:kGroupName]) {
if (assetsFetchResult.count > 0) {
[assetsFetchResult enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
//you have got your image type asset.
if (obj) {
/得到单个PHAsset对象,简单数据处理,装入容器
EBAssetModel * assetModel = [[EBAssetModel alloc]init];
assetModel.assetPH = (PHAsset *)obj;
}
if (idx == 0) {
//处理图片数据,排序
[self performSelectorOnMainThread:@selector(finishGetAllPhotos) withObject:nil waitUntilDone:YES];
}
}];
}
else{
/去掉菊花视图
NSString * tipMsg = @"图片库暂无图片,先去拍摄一张图片吧。";
[UIToastView showToastViewWithContent:tipMsg andRect:CGRectMake(40, 100, SCREEN_WIDTH - 40*2, 40) andTime:1.5f andObject:self];
}
}
}
}
- (void)finishGetAllPhotos
{
//获取数据成功,数据加工成可以满足项目需求为止,然后创建视图,刷新即可
}
注意点:
容器中装的是EBAssetModel的model
导入2个库AssetsLibrary/AssetsLibrary.h>和Photos/Photos.h>
@interface EBAssetModel : NSObject
@property (nonatomic, strong) ALAsset * asset;
@property (nonatomic, strong) PHAsset * assetPH;
@end
首先获取单个对象模型然后获取图片
EBAssetModel * assetModel = ...
1.缩略图
__block UIImage * image = nil;
if (iOS(8)) {
CGFloat kThumbnailW = kW_PICTURE;
CGFloat kThumbnailH = kH_PICTURE;
PHImageRequestOptions * requestOptions = [[PHImageRequestOptions alloc]init];
requestOptions.synchronous = YES;
requestOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
[[PHImageManager defaultManager] requestImageForAsset:assetModel.assetPH
targetSize:CGSizeMake(kThumbnailW, kThumbnailH)
contentMode:PHImageContentModeAspectFill
options:requestOptions
resultHandler:^(UIImage *result, NSDictionary *info) {
image = result;
}];
}else{
image = [UIImage imageWithCGImage:[assetModel.asset thumbnail]];
}
2.高清图
__block UIImage * imageOriginal = nil;if (iOS(8)) {
[self.view showActivityViewAtCenter];
PHImageRequestOptions * requestOptions = [[PHImageRequestOptions alloc]init];
requestOptions.synchronous = YES;
requestOptions.networkAccessAllowed = YES;
requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
[[PHImageManager defaultManager] requestImageForAsset:assetModel.assetPH
targetSize:PHImageManagerMaximumSize
contentMode:PHImageContentModeAspectFill
options:requestOptions
resultHandler:^(UIImage *result, NSDictionary *info) {
imageOriginal = result;
}];
}else{
imageOriginal = [UIImage imageWithCGImage:[assetModel.asset.defaultRepresentation fullScreenImage]];
}