相機相冊

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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末峻村,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子钠糊,更是在濱河造成了極大的恐慌挟秤,老刑警劉巖壹哺,帶你破解...
    沈念sama閱讀 222,378評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件抄伍,死亡現(xiàn)場離奇詭異,居然都是意外死亡管宵,警方通過查閱死者的電腦和手機截珍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來箩朴,“玉大人岗喉,你說我怎么就攤上這事≌ㄅ樱” “怎么了钱床?”我有些...
    開封第一講書人閱讀 168,983評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長埠居。 經(jīng)常有香客問我查牌,道長,這世上最難降的妖魔是什么滥壕? 我笑而不...
    開封第一講書人閱讀 59,938評論 1 299
  • 正文 為了忘掉前任纸颜,我火速辦了婚禮,結(jié)果婚禮上绎橘,老公的妹妹穿的比我還像新娘胁孙。我一直安慰自己,他們只是感情好称鳞,可當(dāng)我...
    茶點故事閱讀 68,955評論 6 398
  • 文/花漫 我一把揭開白布涮较。 她就那樣靜靜地躺著,像睡著了一般冈止。 火紅的嫁衣襯著肌膚如雪法希。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,549評論 1 312
  • 那天靶瘸,我揣著相機與錄音苫亦,去河邊找鬼毛肋。 笑死,一個胖子當(dāng)著我的面吹牛屋剑,可吹牛的內(nèi)容都是我干的润匙。 我是一名探鬼主播,決...
    沈念sama閱讀 41,063評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼唉匾,長吁一口氣:“原來是場噩夢啊……” “哼孕讳!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起巍膘,我...
    開封第一講書人閱讀 39,991評論 0 277
  • 序言:老撾萬榮一對情侶失蹤厂财,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后峡懈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體璃饱,經(jīng)...
    沈念sama閱讀 46,522評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,604評論 3 342
  • 正文 我和宋清朗相戀三年肪康,在試婚紗的時候發(fā)現(xiàn)自己被綠了荚恶。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,742評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡磷支,死狀恐怖谒撼,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情雾狈,我是刑警寧澤廓潜,帶...
    沈念sama閱讀 36,413評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站善榛,受9級特大地震影響辩蛋,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜锭弊,卻給世界環(huán)境...
    茶點故事閱讀 42,094評論 3 335
  • 文/蒙蒙 一堪澎、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧味滞,春花似錦樱蛤、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蚁署,卻和暖如春便脊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背光戈。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評論 1 274
  • 我被黑心中介騙來泰國打工哪痰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留遂赠,地道東北人。 一個月前我還...
    沈念sama閱讀 49,159評論 3 378
  • 正文 我出身青樓晌杰,卻偏偏與公主長得像跷睦,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子肋演,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,747評論 2 361

推薦閱讀更多精彩內(nèi)容

  • 小區(qū)對面又開了一家水果店抑诸,聽老板口音像是福建人,簡單交談知道是福建漳州人爹殊!晚上路過店門口蜕乡,我對象去買水果,他們...
    小小書童吶閱讀 266評論 0 0
  • 9月11號 星期二 晴 今天我們班導(dǎo)護梗夸,家長們輪流著层玲。下午放學(xué)我去了蘭州路“上崗了”……臨近放學(xué)一群群家長騎著電動...
    楚亦菲媽媽閱讀 86評論 0 0