框架
TZImagePickerController
封装
// .h
#import <Foundation/Foundation.h>
#import "TZImagePickerController.h" // 图片选择器
@interface KapPhotoManager : NSObject<TZImagePickerControllerDelegate>
@property (nonatomic,strong) NSIndexPath *indexPath;// 记录插入的位置
@property (nonatomic,copy) void(^finshBlock)(NSArray *photos,NSArray *assets,NSIndexPath *indexPath);
- (TZImagePickerController *)CreatedImagePickerController;
- (void)setFinshBlock:(void (^)(NSArray *photos, NSArray *assets, NSIndexPath *indexPath))finshBlock;
+ (NSString *)filePathByAssets:(id)assert AndImage:(UIImage *)image;
@end
// .m
#import "KapPhotoManager.h"
#import "TZImageManager.h"
#import <AssetsLibrary/AssetsLibrary.h>
@implementation KapPhotoManager
- (TZImagePickerController *)CreatedImagePickerController{
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 columnNumber:4 delegate:self pushPhotoPickerVc:YES];
imagePickerVc.allowTakePicture = YES;// 在内部显示拍照按钮
imagePickerVc.sortAscendingByModificationDate = YES;// 照片排列按修改时间升序
imagePickerVc.allowPickingVideo = NO;
imagePickerVc.allowPickingImage = YES;
imagePickerVc.allowPickingGif = NO;
imagePickerVc.allowPickingOriginalPhoto = YES;//允许选择原图
return imagePickerVc;
}
#pragma mark - TZImagePickerControllerDelegate
- (void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker {
// NSLog(@"cancel");
}
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
//获得原图
// [[TZImageManager manager] getOriginalPhotoWithAsset:<#(id)#> completion:<#^(UIImage *photo, NSDictionary *info)completion#>];
if (self.finshBlock) {
self.finshBlock(photos,assets,self.indexPath);
}
}
// 如果用户选择了一个视频,下面的handle会被执行
// 如果系统版本大于iOS8,asset是PHAsset类的对象,否则是ALAsset类的对象
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(id)asset{
}
// 如果用户选择了一个gif图片,下面的handle会被执行
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingGifImage:(UIImage *)animatedImage sourceAssets:(id)asset {
}
// 决定相册显示与否
- (BOOL)isAlbumCanSelect:(NSString *)albumName result:(id)result {
return YES;
}
// 决定asset显示与否
- (BOOL)isAssetCanSelect:(id)asset {
return YES;
}
#pragma mark 图片存储相关
+ (NSString *)filePathByAssets:(id)assert AndImage:(UIImage *)image{
NSString *imageName = [self imageNameSha512ByAssert:assert];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:imageName];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
[imageData writeToFile:imagePath atomically:YES];
return imagePath;
}
+ (NSString *)imageNameSha512ByAssert:(id)asset{
NSString *imageName;
if ([asset isKindOfClass:[PHAsset class]]) {
PHAsset *phAsset = (PHAsset *)asset;
imageName = phAsset.localIdentifier;
} else if ([asset isKindOfClass:[ALAsset class]]) {
ALAsset *alAsset = (ALAsset *)asset;
imageName = alAsset.defaultRepresentation.UTI;
}
if ([NSString isBlankString:imageName]) {
imageName = [[NSUUID UUID] UUIDString];
}
NSString *fileName = [NSString stringWithFormat:@"%@.jpg",[NSString sha512:imageName]];// 将图片名字share512加密作为文件名
return fileName;
}
@end