RGImagePicker
RGImagePicker 是一个组件,可以帮助你优雅地选择图片、GIF、视频和LivePhoto
功能
-
如系统“照片”应用般的切换动画和交互手势。在 RGImageGallery 中演示
-
选择图片
-
长按显示 presentingViewController
-
选择 GIF
-
选择视频
-
选择 LivePhoto
-
支持iCloud
-
快速滑动时性能高(在演示中有16000张图片)
-
可自定义选择资源类型
-
可自定义选择界面
安装
通过添加以下代码到 Podfile 使用 CocoaPods 进行安装
pod 'RGImagePicker'
用法
导入
#import <RGImagePicker/RGImagePicker.h>
创建PickerConfig
+ (RGImagePickerConfig *)onlyImageConfigWithBackgroundImage:(UIImage *)backgroundImage {
RGImagePickerConfig *config = [RGImagePickerConfig new];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"mediaType = %d", PHAssetMediaTypeImage];
PHFetchOptions *option = [[PHFetchOptions alloc] init];
option.predicate = predicate;
config.option = option;
config.backgroundImage = backgroundImage;
config.backgroundBlurRadius = 3.5;
config.tintColor = [UIColor blackColor];
config.privacyDescriptionString = @"App 没有权限访问您的相册\n\n请前往设置 打开权限";
/*
// Cutom smart album list if need
NSMutableArray *array = [NSMutableArray array];
[array addObject:@(PHAssetCollectionSubtypeSmartAlbumUserLibrary)];
[array addObject:@(PHAssetCollectionSubtypeSmartAlbumFavorites)];
[array addObject:@(PHAssetCollectionSubtypeSmartAlbumTimelapses)];
[array addObject:@(PHAssetCollectionSubtypeSmartAlbumRecentlyAdded)];
if (@available(iOS 10.3, *)) {
[array addObject:@(PHAssetCollectionSubtypeSmartAlbumLivePhotos)];
}
[array addObject:@(PHAssetCollectionSubtypeSmartAlbumPanoramas)];
if (@available(iOS 9.0, *)) {
[array addObject:@(PHAssetCollectionSubtypeSmartAlbumSelfPortraits)];
}
config.cutomSmartAlbum = array;
// Push into this album at first
config.defaultType = PHAssetCollectionSubtypeSmartAlbumFavorites;
*/
return config;
}
显示Picker
RGImagePickerConfig *config = [self onlyImageConfigWithBackgroundImage:self.backgroundView.image];
[RGImagePicker presentByViewController:self maxCount:10 config:config pickResult:^(NSArray<PHAsset *> * _Nonnull phassets, UIViewController * _Nonnull pickerViewController) {
// Began multi load resource
}];
自行展示
RGImagePickerConfig *config = [self onlyImageConfigWithBackgroundImage:self.backgroundView.image];
UINavigationController *nvc = [RGImagePicker pickerWithMaxCount:10 config:config pickResult:^(NSArray<PHAsset *> * _Nonnull phassets, UIViewController * _Nonnull pickerViewController) {
// Began multi load resource
}];
nvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:nvc animated:YES completion:nil];
加载资源
// LoadOption tell picker load rules
RGImagePickerLoadOption option = RGImagePickerLoadVideoFirst|RGImagePickerLoadVideoMediumQuality|RGImagePickerLoadNeedLivePhoto;
[RGImagePicker loadResourceFromAssets:phassets
loadOption:option
thumbSize:CGSizeMake(1280, 1280)
completion:^(NSArray<NSDictionary *> * _Nonnull infos, NSError * _Nullable error) {
if (error) {
return;
}
// Handle resource data
// 🙋 There is a example from the demo that Presentation in above GIF
[self.presentingViewController dismissViewControllerAnimated:YES completion:^{
[infos enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull info, NSUInteger idx, BOOL * _Nonnull stop) {
// get resource in info
NSData *imageData = info[RGImagePickerResourceData];
NSData *thumbData = info[RGImagePickerResourceThumbData];
if (!imageData.length) {
return;
}
if (!thumbData.length) {
thumbData = imageData;
}
PHAsset *asset = phassets[idx];
NSString *filename = info[RGImagePickerResourceFilename];
NSString *thumbName = nil;
BOOL isGif = [[info[RGImagePickerResourceUTI] lowercaseString] containsString:@"gif"];
if (isGif) {
thumbName = [asset.localIdentifier stringByAppendingPathComponent:filename];
thumbData = imageData;
} else {
thumbName = [asset.localIdentifier stringByAppendingPathComponent:[NSString stringWithFormat:@"thumb-%@", filename]];
}
// construct message model
RGMessage *model = [RGMessage new];
// write data to path
NSString *path = [CTFileManger.cacheManager createFile:thumbName atFolder:UCChatDataFolderName data:thumbData];
if (!path.length) {
return;
}
model.thumbUrl = thumbName;
model.thumbSize = info[RGImagePickerResourceThumbSize];
filename = [asset.localIdentifier stringByAppendingPathComponent:filename];
if (!isGif) {
path = [CTFileManger.cacheManager createFile:filename atFolder:UCChatDataFolderName data:imageData];
if (!path.length) {
return;
}
}
model.originalImageUrl = filename;
model.originalImageSize = info[RGImagePickerResourceSize];
[self insertChatData:model];
}];
}];
}];
开发中
- 图像编辑
- Picker排序
- 从相机选择