1座柱、創(chuàng)建繼承于UIView的視圖
#import <UIKit/UIKit.h>
@class LMJImageChooseControl;
#define SettingCenterUrl @"prefs:root=com.ArtPollo.Artpollo"
@protocol LMJImageChooseControlDelegate <NSObject>
@optional
- (void)imageChooseControl:(LMJImageChooseControl *)control didChooseFinished:(UIImage *)image;
- (void)imageChooseControl:(LMJImageChooseControl *)control didClearImage:(UIImage *)image;
@end
@interface LMJImageChooseControl : UIView <UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,copy) NSString * pickerTitle;
@property (nonatomic,assign) UIViewController * superViewController;
@property (nonatomic,assign) id<LMJImageChooseControlDelegate> delegate;
@property (nonatomic,strong,readonly) UIImage * image;
@end
2疾党、在.m中進行布局與相機方法
#import "LMJImageChooseControl.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
@implementation LMJImageChooseControl
{
UILabel * _markLabel;
UIButton * _imgBtn;
UIButton * _clearBtn;
}
- (id)init{
self = [super init];
if (self) {
[self initData];
[self buildViews];
}
return self;
}
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self initData];
[self buildViews];
[self setFrames];
}
return self;
}
- (void)setFrame:(CGRect)frame{
[super setFrame:frame];
[self setFrames];
}
- (void)initData{
_pickerTitle = @"選擇";
_image = nil;
}
- (void)buildViews{
self.backgroundColor = [UIColor lightGrayColor];
// 標(biāo)簽
_markLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
// _markLabel.text = @"點擊添加圖片";
_markLabel.textColor = [UIColor blackColor];
_markLabel.textAlignment = NSTextAlignmentCenter;
_markLabel.font = [UIFont systemFontOfSize:15.f];
// _markLabel.backgroundColor = [UIColor clearColor];
[self addSubview:_markLabel];
// 圖片按鈕
_imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_imgBtn setImage:nil forState:UIControlStateNormal];
[_imgBtn addTarget:self action:@selector(clickImgBtnAddImage:) forControlEvents:UIControlEventTouchUpInside];
_imgBtn.backgroundColor = [UIColor redColor];
[self addSubview:_imgBtn];
// 清除按鈕
_clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_clearBtn setImage:[UIImage imageNamed:@"ImageChooseCloseBtn.png"] forState:UIControlStateNormal];
[_clearBtn setHidden:YES];
[_clearBtn addTarget:self action:@selector(clickClearBtn) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_clearBtn];
}
- (void)setFrames{
_markLabel.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
[_imgBtn setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[_clearBtn setFrame:CGRectMake(self.frame.size.width -40, 0, 40, 40)];
}
#pragma mark - ClickBtn Methods
- (void)clickImgBtnAddImage:(UIButton *)button{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:_pickerTitle
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"從相冊選取", @"拍照", nil];
[sheet showInView:self];
}
- (void)clickClearBtn{
[_imgBtn setImage:nil forState:UIControlStateNormal];
[_clearBtn setHidden:YES];
_image = nil;
if ([self.delegate respondsToSelector:@selector(imageChooseControl:didClearImage:)]) {
[self.delegate imageChooseControl:self didClearImage:nil];
}
}
#pragma mark - ActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 2) {
return;
}
if (buttonIndex == 0) {
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
if (author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied){
//無權(quán)限
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"未獲得授權(quán)訪問相冊" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設(shè)置", nil];
[alertView show];
return;
}
}else if (buttonIndex == 1) {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
{
//無權(quán)限
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"未獲得授權(quán)使用攝像頭" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設(shè)置", nil];
[alertView show];
return;
}
}
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
if (buttonIndex == 0) {
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
} else {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self.superViewController presentViewController:imagePicker animated:YES completion:nil];
}
#pragma mark - ImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
[_imgBtn setImage:image forState:UIControlStateNormal];
[_clearBtn setHidden:NO];
_image = image;
if ([self.delegate respondsToSelector:@selector(imageChooseControl:didChooseFinished:)]) {
[self.delegate imageChooseControl:self didChooseFinished:image];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UIAlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:SettingCenterUrl]];
}
}
@end
3.在ViewControl中導(dǎo)入頭文件,調(diào)用方法
#import "ViewController.h"
#import "LMJImageChooseControl.h"
@interface ViewController ()<LMJImageChooseControlDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
LMJImageChooseControl * imgChooseControl = [[LMJImageChooseControl alloc] initWithFrame:CGRectMake(20, 60, 100, 100)];
// UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake( 0, 0, 100, 100)];
// [btn setTitle:@"加圖片" forState:UIControlStateNormal];
// [imgChooseControl addSubview:btn];
imgChooseControl.pickerTitle = @"選擇照片";
imgChooseControl.superViewController = self;
imgChooseControl.delegate = self;
[self.view addSubview:imgChooseControl];
LMJImageChooseControl * imgChooseControl1 = [[LMJImageChooseControl alloc] initWithFrame:CGRectMake(20, 350, 100, 100)];
imgChooseControl1.pickerTitle = @"選擇照片";
imgChooseControl1.superViewController = self;
imgChooseControl1.delegate = self;
[self.view addSubview:imgChooseControl1];
}
- (void)imageChooseControl:(LMJImageChooseControl *)control didChooseFinished:(UIImage *)image{
NSLog(@"Choose Finished!");
}
- (void)imageChooseControl:(LMJImageChooseControl *)control didClearImage:(UIImage *)image{
NSLog(@"Clear!");
}
@end
4故河、代碼中用到了一個圖片:ImageChooseCloseBtn.png