Swift&Objective-CiOS开发calayer及其子类 与动画相关的

iOS 获取系统相册图片

2016-07-09  本文已影响2770人  紧张的牛排

1. UIImagePickerController

1.1 初始化控制器

UIImagePickerController *pickerCtr = [[UIImagePickerController alloc] init];
pickerCtr.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickerCtr.delegate = self;

1.2 打开相册

[self presentViewController:pickerCtr animated:YES completion:nil];

1.3 选择某个图片时系统调用代理方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        //process image
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
}

1.4 取消选择图片时系统调用代理方法

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

2. AssetsLibrary(已经在iOS8抛弃了)

2.1导入头文件

#import <AssetsLibrary/AssetsLibrary.h>

2.2创建AssetsLibrary对象并初始化

//在@interface中
@property (nonatomic, strong) ALAssetsLibrary *photoLibrary;

//在@implementation
- (ALAssetsLibrary *)photoLibrary {
    if (!_photoLibrary) {
        _photoLibrary = [[ALAssetsLibrary alloc] init];
    }
    return _photoLibrary;
}

2.3 获取相册图片信息

ALAssetsGroupLibrary     NS_ENUM_DEPRECATED_IOS(4_0, 9_0) = (1 << 0),         // The Library group that includes all assets.
ALAssetsGroupAlbum       NS_ENUM_DEPRECATED_IOS(4_0, 9_0) = (1 << 1),         // All the albums synced from iTunes or created on the device.
ALAssetsGroupEvent       NS_ENUM_DEPRECATED_IOS(4_0, 9_0) = (1 << 2),         // All the events synced from iTunes.
ALAssetsGroupFaces       NS_ENUM_DEPRECATED_IOS(4_0, 9_0) = (1 << 3),         // All the faces albums synced from iTunes.
ALAssetsGroupSavedPhotos NS_ENUM_DEPRECATED_IOS(4_0, 9_0) = (1 << 4),         // The Saved Photos album.
#if __IPHONE_5_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
ALAssetsGroupPhotoStream NS_ENUM_DEPRECATED_IOS(5_0, 9_0) = (1 << 5),         // The PhotoStream album.
#endif
ALAssetsGroupAll         NS_ENUM_DEPRECATED_IOS(4_0, 9_0) = 0xFFFFFFFF,       // The same as ORing together all the available group types,
//Get all photos assets in the assets group.
allPhotos;
// Get all video assets in the assets group.
allVideos;
// Get all assets in the group.
allAssets;
/**
@param NSIndexSet               需要获取的相册中图片范围
@param NSEnumerationOptions     获取图片的顺序(顺序还是逆序)
//ALAssetsGroupEnumerationResultsBlock的参数
@param result                   照片ALAsset对象
@param index                    当前result在该group相册中的位置,第index位置上
@param *stop                    需要停止的时候 *stop = YES就停止继续运行当前相册group
*/
enumerateAssetsAtIndexes:(NSIndexSet *)indexSet options:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock
- (void)getPhotoAlbumAsset {
    
    [self.photoLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        
        //只读图片
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];
        //该相册照片数量
        NSInteger photoNumber = [group numberOfAssets];
        
        NSIndexSet *IndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, photoNumber)];
        [group enumerateAssetsAtIndexes:IndexSet options:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
            
            if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                
                ALAssetRepresentation *representation = [result defaultRepresentation];
                NSURL *photoUrl = [representation url];
                NSDate *imageDate = [result valueForProperty:ALAssetPropertyDate];

                //缩略图
                UIImage *image1 = [UIImage imageWithCGImage:result.thumbnail];
                //屏幕分辨率图
                UIImage *image2 = [representation fullScreenImage];
                //原图
                UIImage *image3 = [representation fullScreenImage];
            }
        }];
        
        if (group == NULL) {
            NSLog(@"读取所有相册结束");
        }
        
    } failureBlock:^(NSError *error) {
        NSLog(@"获取相册图片失败");
    }];
}
@autoreleasepool  {
      metadata = [representation metadata]; 
}
[self.photoLibrary assetForURL:photoUrl resultBlock:^(ALAsset *asset) {
     
      ALAssetRepresentation *representation = [result defaultRepresentation]; 
      NSURL *photoUrl = [representation url]; 
      //缩略图 
      UIImage *image1 = [UIImage imageWithCGImage:result.thumbnail]; 
      //屏幕分辨率图 
      UIImage *image2 = [representation fullScreenImage]; 
      //原图 
      UIImage *image3 = [representation fullScreenImage];
  } failureBlock:^(NSError *error) {
}];

3. Photos

3.1导入头文件
#import <Photos/Photos.h>

3.2 设置需要检索的相册类型配置,需要照片或视频在这里配置,或者隐藏的照片等等

PHFetchOptions *options = [PHFetchOptions new];
PHFetchResult *topLevelUserCollections = [PHAssetCollection fetchTopLevelUserCollectionsWithOptions:options];
PHAssetCollectionSubtype subType = PHAssetCollectionSubtypeAlbumRegular;
PHFetchResult *smartAlbumsResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
                                                                                subtype:subType
                                                                                options:options];

3.3 遍历出需要的相册分类

NSMutableArray *photoGroups = [NSMutableArray array];
[topLevelUserCollections enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    if ([obj isKindOfClass:[PHAssetCollection class]]) {
         PHAssetCollection *asset = (PHAssetCollection *)obj;
         PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:asset options:[PHFetchOptions new]];
         if (result.count > 0) {
             PhotoAssetGroup *g = [PhotoAssetGroup groupWithName:asset.localizedTitle fetchResult:result];
             [photoGroups addObject:g];
         }
     }
}];

[smartAlbumsResult enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    if ([obj isKindOfClass:[PHAssetCollection class]]) {
       PHAssetCollection *asset = (PHAssetCollection *)obj;
       PHFetchOptions *options = [[PHFetchOptions alloc] init];
       options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
            
       PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:asset options:options];
       if(result.count > 0 && asset.assetCollectionSubtype != PHAssetCollectionSubtypeSmartAlbumVideos) {
           PhotoAssetGroup *g = [PhotoAssetGroup groupWithName:asset.localizedTitle fetchResult:result];
           [photoGroups addObject:g];
        }
     }
}];

3.4 根据相册分类查找所有照片信息

- (NSArray<PhotoAsset *> *)fetchPhotos:(NSArray<PhotoAssetGroup *> *)groups {
    NSMutableArray *photoAssets = [NSMutableArray array];
    for (PhotoAssetGroup *g in groups) {
        __weak typeof(self)weakSelf = self;
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, g.result.count)];
        [g.result enumerateObjectsAtIndexes:indexSet options:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            PHAsset *asset = (PHAsset*)obj;
            if (asset.mediaType == PHAssetMediaTypeImage) {
                if (![weakSelf.imageProcessor checkAlreadyProcessed:asset.localIdentifier]) {
                    PhotoAsset *pAsset = [PhotoAsset assetWthLocaleIdentifier:asset.localIdentifier creationDate:asset.creationDate];
                    if (![photoAssets containsObject:pAsset]) {
                        [photoAssets addObject:pAsset];
                    }
                }
            }
        }];
    }
    
    [photoAssets sortWithOptions:NSSortStable usingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        PhotoAsset *asset1 = obj1;
        PhotoAsset *asset2 = obj2;
        return ([asset1.creationDate compare:asset2.creationDate] == NSOrderedAscending);
    }];
    return photoAssets;
}

3.5 PhotoAssetPhotoAssetGroup为自定义类

@interface PhotoAsset : NSObject

@property (nonatomic, copy, readonly) NSString *localIdentifier;
@property (nonatomic, strong) NSDate *creationDate;

- (PhotoAsset *)initWithLocaleIdentifier:(NSString *)identifier creationDate:(NSDate *)date;
+ (PhotoAsset *)assetWthLocaleIdentifier:(NSString *)identifier creationDate:(NSDate *)date;

@end



@interface PhotoAssetGroup : NSObject

@property (nonatomic, copy) NSString *groupName;
@property (nonatomic, strong) PHFetchResult *result;

- (PhotoAssetGroup *)initWithName:(NSString *)name fetchResult:(PHFetchResult *)result;
+ (PhotoAssetGroup *)groupWithName:(NSString *)name fetchResult:(PHFetchResult *)result;

@end
上一篇下一篇

猜你喜欢

热点阅读