参考:https://www.cnblogs.com/cqb-learner/p/5792292.html
https://www.jianshu.com/p/140f8996279e
类方法
获得想要的相册集合,继承自PHCollection.类方法
对PHCollection对象进行遍历,获得存放Asset对象的结果集,可以直接获得资源的规格数据,若想获得图片以及原图等资源,需要配合PHImageManager对象,继承自PHObject./**
* 获得所有相簿的原图
*/
- (void)getOriginalImages
{
// 获得所有的自定义相簿
PHFetchResult *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
// 遍历所有的自定义相簿
for (PHAssetCollection *assetCollection in assetCollections) {
[self enumerateAssetsInAssetCollection:assetCollection original:YES];
}
// 获得相机胶卷
PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject;
[self enumerateAssetsInAssetCollection:cameraRoll original:YES];
}
/**
* 获得所有相簿中的缩略图
*/
- (void)getThumbnailImages
{
// 获得所有的自定义相簿
PHFetchResult *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
// 遍历所有的自定义相簿
for (PHAssetCollection *assetCollection in assetCollections) {
[self enumerateAssetsInAssetCollection:assetCollection original:NO];
}
// 获得相机胶卷
PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject;
[self enumerateAssetsInAssetCollection:cameraRoll original:NO];
}
/**
* 遍历相簿中的所有图片
*
* @param assetCollection 相簿
* @param original 是否要原图
*/
- (void)enumerateAssetsInAssetCollection:(PHAssetCollection *)assetCollection original:(BOOL)original
{
NSLog(@"相簿名:%@", assetCollection.localizedTitle);
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
// 同步获得图片, 只会返回1张图片
// options.synchronous = YES;
// 获得某个相簿中的所有PHAsset对象
PHFetchResult *assets = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
for (PHAsset *asset in assets) {
// 是否要原图
CGSize size = original ? CGSizeMake(asset.pixelWidth, asset.pixelHeight) : CGSizeZero;
// 从asset中获得图片
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
NSLog(@"%@", result);
[self.imageArr addObject:result];//把照片存发放到数组,然后用collectionview展示出来,可以做成多选相册
}];
}
[self setCollectionView];
}
/**
* 获得相机胶卷中的所有图片
*/
- (void)getImagesFromCameraRoll
{
// 获得相机胶卷中的所有图片
PHFetchResult *assets = [PHAsset fetchAssetsWithOptions:nil];
__block int count = 0;
for (PHAsset *asset in assets) {
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(asset.pixelWidth, asset.pixelHeight) contentMode:PHImageContentModeDefault options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
}];
}
}
相册多选框架:https://github.com/questbeat/QBImagePicker
//相册多选
-(void)AlumLibrary{
QBImagePickerController *imagePickerController = [QBImagePickerController new];
imagePickerController.delegate = self;
imagePickerController.allowsMultipleSelection = YES;//是否可以多选
imagePickerController.maximumNumberOfSelection = 6;//最大选择数
imagePickerController.minimumNumberOfSelection=3;//最小选择数
//设置每行显示的图像数量
imagePickerController.numberOfColumnsInPortrait = 4;//竖屏下每行4个
imagePickerController.numberOfColumnsInLandscape = 7;//横惬意下每行7个
imagePickerController.showsNumberOfSelectedAssets = YES;//底部显示的选择了几张图片
imagePickerController.prompt = @"Select the photos you want to upload!";//最顶部的那一个行字
[self presentViewController:imagePickerController animated:YES completion:NULL];
}
-(void)photoBrowserDidFinishModalPresentation:(MWPhotoBrowser *)photoBrowser{
NSLog(@"dfdscfd");
}
//确认选择照片完毕
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets {
for (PHAsset *asset in assets) {
}
NSLog(@"确认选择照片完毕");
[self dismissViewControllerAnimated:YES completion:NULL];
}
//取消选择
- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (BOOL)qb_imagePickerController:(QBImagePickerController *)imagePickerController shouldSelectAsset:(PHAsset *)asset{
return YES;
}
//选中图片
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAsset:(PHAsset *)asset{
NSLog(@"选择了图片");
}
//取消选中图片
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didDeselectAsset:(PHAsset *)asset{
NSLog(@"取消选择图片");
}
相册多选框架:
TZLocationManager:https://www.jianshu.com/p/8d4a2d4543ae
相册多选框架:TZImagePickerController:https://github.com/banchichen/TZImagePickerController
//相册多选
-(void)AlumLibrary{
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
// 你可以通过block或者代理,来得到用户选择的照片.
[imagePickerVc setDidFinishPickingVideoHandle:^(UIImage *coverImage, id asset) {
}];
[self presentViewController:imagePickerVc animated:YES completion:nil];
}
//选择完照片
-(void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray *)infos{
NSLog(@"选择完照片");
}
//取消
-(void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker{
NSLog(@"取消");
}
//选择完视频
-(void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(id)asset{
NSLog(@"选择完视频");
}
图片浏览框架:https://github.com/mwaterfall/MWPhotoBrowser
-(void)mbprogress{
[self.photos addObject: [[MWPhoto alloc]initWithImage:self.imageArr[0]]];
[self.photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:@"https://www.baidu.com/img/baidu_jgylogo3.gif"]]];
[self.photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:@"https://www.baidu.com/img/baidu_jgylogo3.gif"]]];
MWPhoto *video = [MWPhoto photoWithURL:[NSURL URLWithString:@"http://www.ddb.cn/public/apk/spjc.mp4"]];
video.videoURL = [[NSURL alloc] initWithString:@"http://www.ddb.cn/public/apk/spjc.mp4"];
[self.photos addObject:video];
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
browser.delegate=self;
browser.displayActionButton = YES;
browser.displayNavArrows = NO;
browser.displaySelectionButtons = NO;
browser.zoomPhotosToFill = YES;
browser.alwaysShowControls = YES;
browser.enableGrid = YES;
browser.startOnGrid = YES;
browser.autoPlayOnAppear = NO;
browser.customImageSelectedIconName = @"eightewm";
browser.customImageSelectedSmallIconName = @"eightewm";
[browser setCurrentPhotoIndex:0];
[browser showNextPhotoAnimated:YES];
[browser showPreviousPhotoAnimated:YES];
// Present
[self.navigationController pushViewController:browser animated:YES];
}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return self.photos.count;
}
- (id )photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
if (index < self.photos.count) {
return [self.photos objectAtIndex:index];
}
return nil;
}
//调用下面的方法会网格显示,网格显示的时候也可以切换到大图
- (id )photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index{
return [_photos objectAtIndex:index];
}
//当点击右下角的分享按钮的时候触下面的方法
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index {
NSLog(@"点击you下角分享按钮的时候出发,这里可以做自己的事情");
}
图片浏览框架:https://github.com/ko1o/PYPhotoBrowser
//图片未发布,本地浏览
-(void)localImage{
[self.imageArr addObject:[UIImage imageNamed:@"eightewm"]];
[self.imageArr addObject:[UIImage imageNamed:@"firstewm"]];
[self.imageArr addObject:[UIImage imageNamed:@"fiveewm"]];
[self.imageArr addObject:[UIImage imageNamed:@"eightewm"]];
// 2.1 设置本地图片
PYPhotosView *photosView = [PYPhotosView photosViewWithImages:self.imageArr];
// 3. 设置代理
photosView.delegate = self;
// 4. 添加photosView
[self.view addSubview:photosView];
}
//图片浏览
-(void)liulanImgae{
//图片浏览需要传入的是UIImageView数组,未发布状态预览传入的是UIImage数组
UIImageView *imgeV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"eightewm"]];
imgeV.frame=CGRectMake(0, 0, 100, 100);
UIImageView *imgeV1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"firstewm"]];
imgeV.frame=CGRectMake(0, 0, 100, 100);
UIImageView *imgeV2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fiveewm"]];
imgeV.frame=CGRectMake(0, 0, 100, 100);
UIImageView *imgeV3=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"eightewm"]];
imgeV.frame=CGRectMake(0, 0, 100, 100);
[self.imageArr addObject:imgeV];
[self.imageArr addObject:imgeV1];
[self.imageArr addObject:imgeV2];
[self.imageArr addObject:imgeV3];
// 1. 创建photoBroseView对象
PYPhotoBrowseView *photoBroseView = [[PYPhotoBrowseView alloc] init];
// 2.1 设置图片源(UIImageView)数组
photoBroseView.sourceImgageViews = self.imageArr;
// 2.2 设置初始化图片下标(即当前点击第几张图片)
photoBroseView.currentIndex = 2;
// 3.显示(浏览)
[photoBroseView show];
}
//浏览网络图片
-(void)lookNetImage{
// 1. 创建缩略图图片链接数组
NSMutableArray *thumbnailImageUrls = [NSMutableArray array];
// 添加图片(缩略图)链接
[thumbnailImageUrls addObject:@""];
[thumbnailImageUrls addObject:@""];
[thumbnailImageUrls addObject:@""];
// 1.2 创建原图图片链接数组
NSMutableArray *originalImageUrls = [NSMutableArray array];
// 添加图片(原图)链接
[originalImageUrls addObject:@""];
[originalImageUrls addObject:@""];
[originalImageUrls addObject:@""];
// 2. 创建一个photosView
PYPhotosView *photosView = [PYPhotosView photosViewWithThumbnailUrls:thumbnailImageUrls originalUrls:originalImageUrls];
// 3. 添加photosView
[self.view addSubview:photosView];
}
图片浏览框架:https://github.com/longitachi/ZLPhotoBrowser
自定义的裁剪:
-----------自定义裁剪框的步骤:-----------
1.当我们实现了UIImagePickerController的代理后,需要在 imagePickerController:didFinishPickingMediaWithInfo:
imagePickerController.allowsEditing = true; 如果为true,就不会展现我们自己的裁剪视图了,就无法自定义了
2.跳转到自定义的控制器中去,吧从相册中的获取的图片传递到控制器中;
UIImage *originImage = info[@"UIImagePickerControllerOriginalImage"];
ImagePickerCropViewController *vc = [[ImagePickerCropViewController alloc] init];
vc.originImage = originImage;
[picker pushViewController:vc animated:true];
3.在控制器中用一个Imageview显示图片,然后给imageview添加手势;实现缩放;最后保存;
************
#import "ViewController.h"
#import "ImagePickerCropViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)choosePhotot:(id)sender {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// imagePickerController.allowsEditing = true; 如果为true,就不会展现我们自己的裁剪视图了,就无法自定义了
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:true completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *originImage = info[@"UIImagePickerControllerOriginalImage"];
ImagePickerCropViewController *vc = [[ImagePickerCropViewController alloc] init];
vc.originImage = originImage;
[picker pushViewController:vc animated:true];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:true completion:nil];
}
@end
#import
@interface ImagePickerCropViewController : UIViewController
@property (nonatomic, strong) UIImage *originImage;
@end
#import "ImagePickerCropViewController.h"
@interface ImagePickerCropViewController ()
@property (nonatomic, strong) UIImageView *cropImageView;
@end
@implementation ImagePickerCropViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
self.navigationItem.rightBarButtonItem = item;
CGRect frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 44);
self.cropImageView = [[UIImageView alloc] initWithFrame:frame];
_cropImageView.contentMode = UIViewContentModeScaleAspectFit;
[_cropImageView setImage:self.originImage];
_cropImageView.userInteractionEnabled = true;
[self.view addSubview:_cropImageView];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.cropImageView addGestureRecognizer:pan];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[self.cropImageView addGestureRecognizer:pinch];
UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
maskView.center = self.view.center;
maskView.backgroundColor = [UIColor clearColor];
maskView.layer.borderWidth = 0.5;
maskView.layer.borderColor = [UIColor whiteColor].CGColor;
[self.view addSubview:maskView];
}
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer {
CGFloat scale = recognizer.scale;
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, scale, scale); //在已缩放大小基础下进行累加变化;区别于:使用 CGAffineTransformMakeScale 方法就是在原大小基础下进行变化
recognizer.scale = 1.0;
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state != UIGestureRecognizerStateEnded && recognizer.state != UIGestureRecognizerStateFailed){
CGPoint translation = [recognizer translationInView:self.view];
CGPoint center = self.cropImageView.center;
self.cropImageView.center = CGPointMake(center.x + translation.x, center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
}
- (UIImage *)captureScreen {
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
img = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(img.CGImage, CGRectMake((self.view.center.x-100)*2+2, (self.view.center.y-100)*2+2, 400-4, 400-4))];
UIGraphicsEndImageContext();
return img;
}
- (void)save {
UIImageWriteToSavedPhotosAlbum([self captureScreen], nil, nil, nil);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
图片裁剪框架:
裁剪LazyDuan/LDImagePicker https://github.com/LazyDuan/LDImagePicker