温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

在IOS中怎么拿到自己相册中得图片

发布时间:2020-08-06 11:54:20 来源:网络 阅读:784 作者:ljl5125 栏目:移动开发


//说明:法1:获取本地相册图片  2:摄像头拍照设为图片

//步骤:一、声明代理<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

//二、声明两个私有的button 一个 UIImageView 分别为:1、获取手机本地相册图片btnLocalLibrary  2、获取拍照图片btnCamera  3 p_w_picpathHead

//三、对声明的控件初始化

//四、实现两个GetLocalPhoto.m里面的三个函数 1-(void)btnSelect1   2-(void)btnSelect2   3-(void)p_w_picpathPickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info



@property (nonatomic, strong)UIButton *btnLocalLibrary;

@property (nonatomic, strong)UIButton *btnCamera;

@property (nonatomic, strong)UIImageView *p_w_picpathHead;




- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    //拿到手机相机,拍照

    self.btnCamera = [UIButton buttonWithType:UIButtonTypeCustom];

    [self.btnCamera setImage:[UIImage p_w_picpathNamed:@"head.jpg"] forState:UIControlStateNormal];

    self.btnCamera.center = CGPointMake(self.view.center.x*1.5, self.view.center.y*1.7);

    self.btnCamera.bounds = CGRectMake(0, 0, 70, 70);

    self.btnCamera.layer.cornerRadius = 35;

    self.btnCamera.layer.borderColor = [UIColor whiteColor].CGColor;

    self.btnCamera.layer.borderWidth = 3.0;

    self.btnCamera.clipsToBounds = YES;

    [self.btnCamera addTarget:self action:@selector(btnSelect1) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.btnCamera];

    

    

    //从相册中获取头像

    self.btnLocalLibrary = [UIButton buttonWithType:UIButtonTypeCustom];

    [self.btnLocalLibrary setImage:[UIImage p_w_picpathNamed:@"head.jpg"] forState:UIControlStateNormal];

    self.btnLocalLibrary.center = CGPointMake(self.view.center.x*0.5, self.view.center.y*1.7);

    self.btnLocalLibrary.bounds = CGRectMake(0, 0, 70, 70);

    self.btnLocalLibrary.layer.cornerRadius = 35;

    self.btnLocalLibrary.layer.borderColor = [UIColor whiteColor].CGColor;

    self.btnLocalLibrary.layer.borderWidth = 3.0;

    self.btnLocalLibrary.clipsToBounds = YES;

    [self.btnLocalLibrary addTarget:self action:@selector(btnSelect2) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.btnLocalLibrary];


    

    

    //头像

    self.p_w_picpathHead = [[UIImageView alloc]initWithFrame:CGRectMake(130, 100, 60, 60)];

    self.p_w_picpathHead.p_w_picpath = [UIImage p_w_picpathNamed:@"head.jpg"];

    [self.view addSubview:self.p_w_picpathHead];

    

}



#pragma mark -摄像头拍照的图片

-(void)btnSelect1

{

    //判断是否可以使用摄像头

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

    {

        //打开摄像头

        UIImagePickerController * picker = [[UIImagePickerController alloc]init];

        picker.delegate = self;

        picker.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController:picker animated:YES completion:nil];

    }

    else

    {

        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"不能使用照相机" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

        

        [alert show];

        

    }

  

}



#pragma mark -拿已经存在手机相册里的图片

-(void)btnSelect2

{

//    UIImagePickerController 

    //判断是否可以使用相册

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

        UIImagePickerController * picker = [[UIImagePickerController alloc]init];

        picker.delegate = self;

        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        

        [self presentViewController:picker  animated:YES completion:nil];

    }else

    {

        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"不能使用相册" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

        

        [alert show];

    }

    

}


-(void)p_w_picpathPickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    //将照片显示在屏幕上

    //获取当前拍摄的照片

    UIImage * p_w_picpath = [info valueForKey:UIImagePickerControllerOriginalImage];

    self.p_w_picpathHead.p_w_picpath = p_w_picpath;

    UIImage * p_w_picpath2 = [info valueForKey:UIImagePickerControllerOriginalImage];

    self.p_w_picpathHead.p_w_picpath = p_w_picpath2;

    //将照片存放到相册当中

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        UIImageWriteToSavedPhotosAlbum(p_w_picpath, p_w_picpath2,nil, nil);

    }

    [self dismissViewControllerAnimated:YES completion:nil];

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI