承接上一文吭产,這篇是選擇圖片的文章,具體代碼如下
有必要說一下的是isRemovePic是判斷當(dāng)前是否是刪除照片鸭轮;imageArray是用戶選擇的相片數(shù)組
值得一提的是臣淤,我采用了CTAssetsPickerController圖片選擇器,貌似這個(gè)作者現(xiàn)在不再維護(hù)這份代碼了窃爷,而也沒能在GitHub上找到更好的圖片選擇器邑蒋,所以各位同學(xué)如果有更好的圖片選擇器,請(qǐng)一定要告訴我噢按厘!
/** 加載照片*/
- (void)setImage {
/** 如果是通過刪除照片的方法進(jìn)到加載照片就不添加加號(hào)圖片進(jìn)去*/
if (!self.isRemovePic) {
UIImage *addImage = [UIImage imageNamed:@"PicUpload_add"];
[self.imageArray addObject:addImage];
}
self.isRemovePic = NO;
for (NSInteger i = 0; i < [self.imageArray count]; i++) {
CGFloat letterWidth = (SCREEN_WIDTH - 6 * LINKHOME_CUSTOM_DISTANCE_TEN) / 5;
CGFloat imageViewX = i * (letterWidth + LINKHOME_CUSTOM_DISTANCE_TEN) + LINKHOME_CUSTOM_DISTANCE_TEN;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageViewX, 0, letterWidth, letterWidth)];
imageView.userInteractionEnabled = YES;
imageView.image = self.imageArray[i];
imageView.tag = kImageTag + i;
/** 因?yàn)樽詈笠粋€(gè)一定是添加的圖片医吊,所以給最后一個(gè)添加一個(gè)點(diǎn)擊添加手勢(shì)*/
if (i == [self.imageArray count] - 1) {
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openPhoto)];
[imageView addGestureRecognizer:tapGes];
} else { /** 為圖片添加一個(gè)右上角的刪除按鈕并且添加一個(gè)點(diǎn)擊顯示大圖的手勢(shì)*/
UIButton *removeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
CGFloat removeX = letterWidth * 4 / 5;
CGFloat removeW = letterWidth / 5;
removeBtn.frame = CGRectMake(removeX, 0, removeW, removeW);
[removeBtn setImage:[UIImage imageNamed:@"PicUpload_remove"] forState:UIControlStateNormal];
[removeBtn addTarget:self action:@selector(removeImage:) forControlEvents:UIControlEventTouchUpInside];
removeBtn.tag = i;
[imageView addSubview:removeBtn];
HLFeedBackImageTapGestureRecongnizer *tapGes = [[HLFeedBackImageTapGestureRecongnizer alloc] initWithTarget:self action:@selector(tapImage:)];
tapGes.imageArray = self.imageArray;
[imageView addGestureRecognizer:tapGes];
}
[self addSubview:imageView];
}
}
/** 打開相冊(cè)*/
- (void)openPhoto {
/** 判斷當(dāng)前授權(quán)狀態(tài)*/
PHAuthorizationStatus oldStatus = [PHPhotoLibrary authorizationStatus];
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
switch (status) {
case PHAuthorizationStatusRestricted: /** 系統(tǒng)級(jí)別的控制(如家長(zhǎng)控制)*/
break;
case PHAuthorizationStatusDenied: /** 用戶選擇了取消*/
if (oldStatus != PHAuthorizationStatusNotDetermined) {
break;
}
break;
case PHAuthorizationStatusAuthorized: /** 當(dāng)前用戶允許app訪問相冊(cè)*/
[self choosePhotos];
default:
break;
}
}];
}
/** 選擇圖片*/
- (void)choosePhotos {
dispatch_async(dispatch_get_main_queue(), ^{
CTAssetsPickerController *picker = [[CTAssetsPickerController alloc] init];
picker.showsSelectionIndex = YES;
picker.delegate = self;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
picker.modalPresentationStyle = UIModalPresentationFormSheet;
}
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
[window.rootViewController presentViewController:picker animated:YES completion:nil];
});
}
/** 重新加載照片*/
- (void)reload {
/** 首先把當(dāng)前顯示的所有控件都移除*/
for (UIView *view in [self subviews]) {
[view removeFromSuperview];
}
/** 再將數(shù)組中的圖片重新排列出來*/
[self setImage];
}
/** 移除某一張照片*/
- (void)removeImage:(UIButton *)button {
self.isRemovePic = YES;
[self.imageArray removeObjectAtIndex:button.tag];
[self reload];
}
/** 點(diǎn)擊圖片的手勢(shì)*/
- (void)tapImage:(HLFeedBackImageTapGestureRecongnizer *)tapGes{
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *maskview = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
maskview.backgroundColor = [UIColor blackColor];
[window addSubview:maskview];
HLFeedBackShowImageView *fbImageV = [[HLFeedBackShowImageView alloc] initWithFrame:[UIScreen mainScreen].bounds byClick:tapGes.view.tag appendArray:tapGes.imageArray];
[fbImageV show:maskview didFinish:^(){
[UIView animateWithDuration:0.5f animations:^{
fbImageV.alpha = 0.0f;
maskview.alpha = 0.0f;
} completion:^(BOOL finished) {
[fbImageV removeFromSuperview];
[maskview removeFromSuperview];
}];
}];
}
#pragma mark - CTAssetsPickerControllerDelegate
/** 選擇完成時(shí)會(huì)調(diào)用*/
- (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray<PHAsset *> *)assets {
/** 關(guān)閉圖片選擇控制器*/
[picker dismissViewControllerAnimated:YES completion:^{
CGFloat scale = [UIScreen mainScreen].scale;
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeExact;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
/** 遍歷選擇的所有圖片*/
for (NSInteger i = 0; i < assets.count; i++) {
PHAsset *asset = assets[i];
CGSize size = CGSizeMake(asset.pixelWidth / scale, asset.pixelHeight / scale);
/** 獲取圖片*/
[[PHImageManager defaultManager] requestImageForAsset:asset
targetSize:size
contentMode:PHImageContentModeDefault
options:options
resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
[self.imageArray removeLastObject];
[self.imageArray addObject:result];
/** 刷新*/
[self reload];
}];
}
}];
}
/** 相片的最大選擇數(shù)方法*/
- (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset {
NSInteger max = 5;
if (picker.selectedAssets.count + self.imageArray.count - 1 >= max) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:LocatizedStirngForkey(@"notice") message:LocatizedStirngForkey(@"feedback_more_photo") preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:LocatizedStirngForkey(@"dialog_OK") style:UIAlertActionStyleDefault handler:nil]];
[picker presentViewController:alert animated:YES completion:nil];
return NO;
}
return YES;
}
下面的是,在頁(yè)面上展示出選擇圖片后逮京,點(diǎn)擊圖片能夠顯示大圖并且能夠放大縮小滑動(dòng)等:
.h:
typedef void(^didRemoveImage)(void);
@interface HLFeedBackShowImageView : UIView<UIScrollViewDelegate>
{
UIImageView *showImage;
}
@property (nonatomic,copy) didRemoveImage removeImg;
- (void)show:(UIView *)bgView didFinish:(didRemoveImage)tempBlock;
- (id)initWithFrame:(CGRect)frame byClick:(NSInteger)clickTag appendArray:(NSArray *)appendArray;
.m:
#define kImageTag 9999
@implementation HLFeedBackShowImageView
{
UIScrollView *_scrollView;
CGRect self_Frame;
NSInteger page;
BOOL doubleClick;
}
- (id)initWithFrame:(CGRect)frame byClick:(NSInteger)clickTag appendArray:(NSArray *)appendArray {
if ([super initWithFrame:frame]) {
self_Frame = frame;
self.alpha = 0.0f;
page = 0;
doubleClick = YES;
NSMutableArray *array = [NSMutableArray arrayWithArray:appendArray];
[array removeLastObject];
[self configScrollViewWith:clickTag andAppendArray:array];
UITapGestureRecognizer *tapGser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disappear)];
tapGser.numberOfTouchesRequired = 1;
tapGser.numberOfTapsRequired = 1;
[self addGestureRecognizer:tapGser];
UITapGestureRecognizer *doubleTapGser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeBig:)];
doubleTapGser.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTapGser];
[tapGser requireGestureRecognizerToFail:doubleTapGser];
}
return self;
}
- (void)configScrollViewWith:(NSInteger)clickTag andAppendArray:(NSArray *)appendArray {
_scrollView = [[UIScrollView alloc] initWithFrame:self_Frame];
_scrollView.backgroundColor = [UIColor blackColor];
_scrollView.pagingEnabled = true;
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(self.frame.size.width * appendArray.count, 0);
[self addSubview:_scrollView];
float W = self.frame.size.width;
for (int i = 0; i < appendArray.count; i ++) {
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
imageScrollView.backgroundColor = [UIColor blackColor];
imageScrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
imageScrollView.delegate = self;
imageScrollView.maximumZoomScale = 4;
imageScrollView.minimumZoomScale = 1;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
UIImage *img = [appendArray objectAtIndex:i];
imageView.image = img;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageScrollView addSubview:imageView];
[_scrollView addSubview:imageScrollView];
imageScrollView.tag = 100 + i ;
imageView.tag = 1000 + i;
}
[_scrollView setContentOffset:CGPointMake(W * (clickTag - kImageTag), 0) animated:YES];
page = clickTag - kImageTag;
}
- (void)disappear {
_removeImg();
}
- (void)changeBig:(UITapGestureRecognizer *)tapGes {
CGFloat newscale = 1.9;
UIScrollView *currentScrollView = (UIScrollView *)[self viewWithTag:page + 100];
CGRect zoomRect = [self zoomRectForScale:newscale withCenter:[tapGes locationInView:tapGes.view] andScrollView:currentScrollView];
if (doubleClick == YES) {
[currentScrollView zoomToRect:zoomRect animated:YES];
}else {
[currentScrollView zoomToRect:currentScrollView.frame animated:YES];
}
doubleClick = !doubleClick;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
UIImageView *imageView = (UIImageView *)[self viewWithTag:scrollView.tag + 900];
return imageView;
}
- (CGRect)zoomRectForScale:(CGFloat)newscale withCenter:(CGPoint)center andScrollView:(UIScrollView *)scrollV {
CGRect zoomRect = CGRectZero;
zoomRect.size.height = scrollV.frame.size.height / newscale;
zoomRect.size.width = scrollV.frame.size.width / newscale;
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
}
- (void)show:(UIView *)bgView didFinish:(didRemoveImage)tempBlock {
[bgView addSubview:self];
_removeImg = tempBlock;
[UIView animateWithDuration:.4f animations:^{
self.alpha = 1.0f;
}];
}
#pragma mark - ScorllViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
CGPoint offset = _scrollView.contentOffset;
page = offset.x / self.frame.size.width ;
UIScrollView *scrollV_next = (UIScrollView *)[self viewWithTag:page + 100 + 1]; /** 前一頁(yè)*/
if (scrollV_next.zoomScale != 1.0){
scrollV_next.zoomScale = 1.0;
}
UIScrollView *scollV_pre = (UIScrollView *)[self viewWithTag:page + 100 - 1]; /** 后一頁(yè)*/
if (scollV_pre.zoomScale != 1.0){
scollV_pre.zoomScale = 1.0;
}
}