iOS 调用系统相册

iOS调用系统相册显示英文标题:
 在 Info.plist 中
 Localized resources can be mixed  设为 YES,意思是允许应用获取框架库内语言。
 Localization native development region  设为 China

在inof.pilst中的设置

Privacy - Photo Library Additions Usage Description  保存图片到相册
Privacy - Photo Library Usage Description  App需要您的同意,才能访问相册
Privacy - Camera Usage Description App需要您的同意,才能访问相机

代码部分

#import "ViewController.h"
#define kScreenHeight         [[UIScreen mainScreen] bounds].size.height
// 获取屏幕宽度 即:整屏的宽度
#define kScreenWidth            [[UIScreen mainScreen] bounds].size.width
@interface ViewController ()
@property(nonatomic,strong)UIButton * positiveButton;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"首页";
    self.automaticallyAdjustsScrollViewInsets= NO;
    [self creatMainView];
}

- (void)creatMainView{
    _positiveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _positiveButton.frame = CGRectMake((kScreenWidth-320)/2, 80, 320, 185);
    [_positiveButton setImage:[UIImage imageNamed:@"身份证正面"] forState:UIControlStateNormal];
    _positiveButton.backgroundColor = [UIColor grayColor];
    [_positiveButton addTarget:self action:@selector(positiveButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_positiveButton];
}

- (void)positiveButtonClick{
    NSLog(@"正面照片");
    UIActionSheet * actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"从相册选择", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
    [actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"buttonIndex-----%li",(long)buttonIndex);
    if (buttonIndex==0) {
        NSLog(@"拍照");
        [self openCamera];
    }else if (buttonIndex==1){
        NSLog(@"从相册选择");
        [self openPhotoLibrary];
    }
}
/*** 调用照相机 */
- (void)openCamera{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES; //可编辑
    //判断是否可以打开照相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        //摄像头
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:nil];
    }else{
        NSLog(@"没有摄像头");
    }
}
/*** 打开相册  */
-(void)openPhotoLibrary{
    // 进入相册
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:YES completion:^{
            NSLog(@"打开相册");
        }];
    }else{
        NSLog(@"不能打开相册");
    }
}

#pragma mark - UIImagePickerControllerDelegate
// 拍照完成回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = info[@"UIImagePickerControllerOriginalImage"];
    NSLog(@"finish..");
    if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
        //图片存入相册
        [_positiveButton setImage:image forState:0];
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    if(picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary){
        [_positiveButton setImage:image forState:0];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
//进入拍摄页面点击取消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(iOS 调用系统相册)