溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在IOS中怎么拿到自己相冊中得圖片

發(fā)布時間:2020-08-06 11:54:20 來源:網(wǎng)絡(luò) 閱讀:783 作者:ljl5125 欄目:移動開發(fā)


//說明:法1:獲取本地相冊圖片  2:攝像頭拍照設(shè)為圖片

//步驟:一、聲明代理<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

//二、聲明兩個私有的button 一個 UIImageView 分別為:1、獲取手機本地相冊圖片btnLocalLibrary  2、獲取拍照圖片btnCamera  3、 p_w_picpathHead

//三、對聲明的控件初始化

//四、實現(xiàn)兩個GetLocalPhoto.m里面的三個函數(shù) 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 -拿已經(jīng)存在手機相冊里的圖片

-(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

{

    //將照片顯示在屏幕上

    //獲取當(dāng)前拍攝的照片

    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;

    //將照片存放到相冊當(dāng)中

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        UIImageWriteToSavedPhotosAlbum(p_w_picpath, p_w_picpath2,nil, nil);

    }

    [self dismissViewControllerAnimated:YES completion:nil];

}


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI