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 ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@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<UIImagePickerControllerInfoKey, id> *)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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 苏苏菜菜子阅读 200评论 0 0
  • 秋寒露池浅水蟾,金菊娇艳北风寒,孤鹜翩跹翔羽翅,波光潋滟红掌翻。雏鸟唤,芦苇姗,寒鸦振翅梧桐斑,斜倚栏杆乱思绪,君...
    雪落重阳阅读 537评论 36 46
  • 1、现在世界上大多数人都“入网”了,按《第七感》里的观点,每个人都面临一个“三难选择”,在高速、共享和安全之间选两...
    fannie22222阅读 173评论 0 0