IOS调取摄像头和相册并保存图片

1.建立项目。

2.将info.plist文件增加下面内容,不然无法访问:

<key>NSPhotoLibraryUsageDescription</key> 
<string>App需要您的同意,才能访问相册</string> 

<key>NSAppleMusicUsageDescription</key> 
<string>App需要您的同意,才能访问媒体资料库</string>

<key>NSCameraUsageDescription</key>    
<string>cameraDesciption</string>

3.类似于换头像,给圆形按钮添加背景图,点击更换头像。图片截取功能yi完善。

//
//  ViewController.m
//  SheetTest
//
//  Created by lishuai on 2017/8/2.
//  Copyright © 2017年 lishuai. All rights reserved.
//

#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIActionSheetDelegate>
@property (nonatomic, strong) UIImageView * imageView;
@property (nonatomic, strong) UIImagePickerController* imagePicker;
@property (nonatomic, strong) UIImageView* HeadShot;
@property (nonatomic, strong) UIButton *Rightbutton;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self button];
    //_HeadShot = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 160, 160)];
    //_HeadShot.frame = CGRectMake(100, 100, 80, 80);
    //_HeadShot.contentMode = UIViewContentModeScaleAspectFill;
    //[_HeadShot setClipsToBounds:YES];
    //[self.view addSubview: _HeadShot];

}

-(void)button {
     _Rightbutton = [UIButton buttonWithType:UIButtonTypeCustom] ;
    [_Rightbutton setFrame:CGRectMake(100, 100, 100, 100)];
    _Rightbutton.layer.cornerRadius = 100/2;
    _Rightbutton.layer.borderWidth = 0.5;
    _Rightbutton.layer.borderColor = [UIColor whiteColor].CGColor;
    _Rightbutton.layer.masksToBounds = YES;
    //[Rightbutton setTitle:@"上传图片" forState:UIControlStateNormal];
    [_Rightbutton setBackgroundImage:[UIImage imageNamed:@"ic_home_penalty"] forState:UIControlStateNormal];
    [_Rightbutton addTarget:self action:@selector(alert:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_Rightbutton];
}

-(void)alert:(UIBarButtonItem *)itemCamera {
    
    _imagePicker = [[UIImagePickerController alloc] init];
    _imagePicker.delegate = self;
    //显示图片截取框,但是截取后图片功能未写
    _imagePicker.allowsEditing=YES;
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                          message:nil
                                          preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照"
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             
                                                             _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                                                             [self presentViewController:_imagePicker animated:YES completion:nil];
    }];
    UIAlertAction *photoAlbum = [UIAlertAction actionWithTitle:@"从手机相册选择"
                                                         style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * _Nonnull action) {
                                                           _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                                                           [self presentViewController:_imagePicker animated:YES completion:nil];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertController addAction:cameraAction];
    [alertController addAction:photoAlbum];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
//调取选取的图片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
   // UIImage *images = [info valueForKey:UIImagePickerControllerEditedImage];
    UIImage *images =info[UIImagePickerControllerEditedImage];
    //_HeadShot.image = images;
    //NSLog(@"image:%@----info:%a",info);
    [_Rightbutton setBackgroundImage: images forState:UIControlStateNormal];
    //NSData *fileDara = UIImageJPEGRepresentation(images, 1.0);
    //保存到手机
    UIImageWriteToSavedPhotosAlbum(images, nil,nil,nil);
    

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


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值