相機相冊

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中進(jìn)行布局與相機方法

#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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容