首先嗅战,涉及訪問相冊(cè)權(quán)限:
需在“plist”文件里妄田,輸入如下代碼!驮捍!
<!-- 相冊(cè) -->
<key>NSPhotoLibraryUsageDescription</key>
<string>APP需要您的同意,才能訪問相冊(cè)</string> <!-- 描述字符串 ??可隨意填疟呐,但必須填 -->
初次使用:
創(chuàng)建九宮格圖片選擇器視圖:SudokuAddImageView
在“SudokuAddImageView.h”文件:
#import <UIKit/UIKit.h>
@interface SudokuAddImageView : UIView
/** 存儲(chǔ)所有的照片(UIImage) */
@property (nonatomic, strong) NSMutableArray *images;
/** 選擇照片是否有編輯框 */
@property (nonatomic,assign) BOOL allowEdit;
@end
在“SudokuAddImageView.m”文件:
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) //屏寬
#define imageH (SCREEN_WIDTH*210.f/750.f) // 圖片高度
#define imageW (SCREEN_WIDTH*210.f/750.f) // 圖片寬度
#define deleImageWH (SCREEN_WIDTH*35.f/750.f) // 刪除按鈕的寬高
#define kMaxColumn 3 // 每行顯示數(shù)量
#define MaxImageCount 9 // 最多顯示圖片個(gè)數(shù)
#define kAdeleImage @"ic_not_pass" // 刪除按鈕圖片
#define kAddImage @"pic_upload_forTotalBtn" // 添加按鈕圖片
@interface SudokuAddImageView () <UIImagePickerControllerDelegate>
{
//標(biāo)識(shí)被編輯的按鈕 -1 為添加新的按鈕
NSInteger editTag;
}
@end
@implementation SudokuAddImageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *btn = [self createButtonWithImage:kAddImage andSeletor:@selector(addNew:)];
[self addSubview:btn];
}
return self;
}
-(NSMutableArray *)images
{
if (_images == nil) {
_images = [NSMutableArray array];
}
return _images;
}
// 添加新的控件
- (void)addNew:(UIButton *)btn
{
// 標(biāo)識(shí)為添加一個(gè)新的圖片
if (![self deleClose:btn]) {
editTag = -1;
[self callImagePicker];
}
}
// 修改舊的控件
- (void)changeOld:(UIButton *)btn
{
// 標(biāo)識(shí)為修改(tag為修改標(biāo)識(shí))
if (![self deleClose:btn]) {
editTag = btn.tag;
[self callImagePicker];
}
}
// 移除"刪除按鈕"
- (BOOL)deleClose:(UIButton *)btn
{
if (btn.subviews.count == 2) {
[[btn.subviews lastObject] removeFromSuperview];
[self stop:btn];
return YES;
}
return NO;
}
// 調(diào)用圖片選擇器
- (void)callImagePicker
{
UIImagePickerController *pc = [[UIImagePickerController alloc] init];
pc.allowsEditing = _allowEdit?_allowEdit:NO;
pc.delegate = self;
[self.window.rootViewController presentViewController:pc animated:YES completion:nil];
}
// 根據(jù)圖片名稱或者圖片對(duì)象 創(chuàng)建一個(gè)新的顯示控件
- (UIButton *)createButtonWithImage:(id)imageNameOrImage andSeletor : (SEL)selector
{
UIImage *addImage = nil;
if ([imageNameOrImage isKindOfClass:[NSString class]]) {
addImage = [UIImage imageNamed:imageNameOrImage];
} else if([imageNameOrImage isKindOfClass:[UIImage class]]) {
addImage = imageNameOrImage;
}
UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[addBtn setImage:addImage forState:UIControlStateNormal];
[addBtn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
addBtn.tag = self.subviews.count;
// 添加長(zhǎng)按手勢(shì),用作刪除
if(addBtn.tag != 0) {
UILongPressGestureRecognizer *gester = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[addBtn addGestureRecognizer:gester];
}
return addBtn;
}
// 長(zhǎng)按添加刪除按鈕
- (void)longPress : (UIGestureRecognizer *)gester
{
if (gester.state == UIGestureRecognizerStateBegan)
{
UIButton *btn = (UIButton *)gester.view;
UIButton *dele = [UIButton buttonWithType:UIButtonTypeCustom];
dele.bounds = CGRectMake(0, 0, deleImageWH, deleImageWH);
[dele setImage:[UIImage imageNamed:kAdeleImage] forState:UIControlStateNormal];
[dele addTarget:self action:@selector(deletePic:) forControlEvents:UIControlEventTouchUpInside];
dele.frame = CGRectMake(btn.frame.size.width - dele.frame.size.width, 0, dele.frame.size.width, dele.frame.size.height);
[btn addSubview:dele];
[self start : btn];
}
}
// 長(zhǎng)按開始抖動(dòng)
- (void)start : (UIButton *)btn {
double angle1 = -5.0 / 180.0 * M_PI;
double angle2 = 5.0 / 180.0 * M_PI;
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";
anim.values = @[@(angle1), @(angle2), @(angle1)];
anim.duration = 0.25;
// 動(dòng)畫的重復(fù)執(zhí)行次數(shù)
anim.repeatCount = MAXFLOAT;
// 保持動(dòng)畫執(zhí)行完畢后的狀態(tài)
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[btn.layer addAnimation:anim forKey:@"shake"];
}
// 停止抖動(dòng)
- (void)stop : (UIButton *)btn{
[btn.layer removeAnimationForKey:@"shake"];
}
// 刪除圖片
- (void)deletePic : (UIButton *)btn
{
[self.images removeObject:[(UIButton *)btn.superview imageForState:UIControlStateNormal]];
[btn.superview removeFromSuperview];
if ([[self.subviews lastObject] isHidden]) {
[[self.subviews lastObject] setHidden:NO];
}
}
// 對(duì)所有子控件進(jìn)行布局
- (void)layoutSubviews
{
[super layoutSubviews];
NSInteger count = self.subviews.count;
CGFloat btnW = imageW;
CGFloat btnH = imageH;
int maxColumn = kMaxColumn > self.frame.size.width / imageW ? self.frame.size.width / imageW : kMaxColumn;
CGFloat marginX = (self.frame.size.width - maxColumn * btnW) / (count + 1);
CGFloat marginY = marginX;
for (int i = 0; i < count; i++) {
UIButton *btn = self.subviews[i];
CGFloat btnX = (i % maxColumn) * (marginX + btnW) + marginX;
CGFloat btnY = (i / maxColumn) * (marginY + btnH) + marginY;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
}
}
#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image;
if (_allowEdit == YES) { //可編輯
image = info[UIImagePickerControllerEditedImage]; //編輯后的圖片
} else { //不可編輯
image = info[UIImagePickerControllerOriginalImage]; //原始的圖片
}
if (editTag == -1) {
// 創(chuàng)建一個(gè)新的控件
UIButton *btn = [self createButtonWithImage:image andSeletor:@selector(changeOld:)];
[self insertSubview:btn atIndex:self.subviews.count - 1];
[self.images addObject:image];
if (self.subviews.count - 1 == MaxImageCount) {
[[self.subviews lastObject] setHidden:YES];
}
} else {
// 根據(jù)tag修改需要編輯的控件
UIButton *btn = (UIButton *)[self viewWithTag:editTag];
NSInteger index = [self.images indexOfObject:[btn imageForState:UIControlStateNormal]];
[self.images removeObjectAtIndex:index];
[btn setImage:image forState:UIControlStateNormal];
[self.images insertObject:image atIndex:index];
}
// 退出圖片選擇控制器
[picker dismissViewControllerAnimated:YES completion:nil];
}
使用
在ViewController里:
包含頭文件:#import "SudokuAddImageView.h" //頭文件
SudokuAddImageView * addImg_V = [[SudokuAddImageView alloc] init];
//addImg_V.allowEdit = YES; //是否可編輯(含編輯框)
addImg_V.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width);
[self.view addSubview:addImg_V];
效果:
當(dāng)然刪除按鈕勾效,根據(jù)需求添加E膊丁!我使用的是長(zhǎng)按手勢(shì)!
之前的需求是在界面點(diǎn)擊圖片后苇倡,可選擇更換,還是刪除當(dāng)前圖片囤踩!
代碼地址:https://github.com/Goyohol/FunctionRealize/tree/master/SukodePicturePicker