iOS 開發調用相機以及獲取相冊照片功能


  //添加代理方法

@interface MineViewController () <UITableViewDelegate, UITableViewDataSource, PayCellDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate>

 

 

//定義消息框

    UIActionSheet * act =[[UIActionSheet alloc]initWithTitle:@"請選擇圖片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"選擇相冊",@"選擇相機", nil];

    //顯示消息框

    [act showInView:self.view];

 

//消息框代理實現

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    //定義圖片選擇器

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

    //判斷

    switch (buttonIndex) {

        case 0:

            //判斷系統是否允許選擇 相冊

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

                //圖片選擇是相冊(圖片來源自相冊)

                picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

                //設置代理

                picker.delegate=self;

                //模態顯示界面

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

                

            }

            break;

        case 1://判斷系統是否允許選擇 相機

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

                //圖片選擇是相冊(圖片來源自相冊)

                picker.sourceType = UIImagePickerControllerSourceTypeCamera;

                //設置代理

                picker.delegate=self;

                //模態顯示界面

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

            }

            else {

                NSLog(@"不支持相機");

            }

            

            

            break;

            

            

        default:

            break;

    }

}

 

 

//實現圖片選擇器代理

//參數:圖片選擇器  字典參數

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    //通過key值獲取到圖片

    UIImage * image =info[UIImagePickerControllerOriginalImage];

    NSLog(@"image=%@  info=%@",image, info);

    

    //判斷數據源類型

    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {

        

        //設置圖片背景

        [userIconBtn setBackgroundImage:image forState:UIControlStateNormal];

        NSUserDefaults * user = [NSUserDefaults standardUserDefaults];

        NSData* imgData = UIImageJPEGRepresentation(image, 1);

        isPhotoChoose = YES;

        NSLog(@"第一次調用這個方法");

        [user setObject:imgData forKey:@"saveIcon"];

        [user synchronize];

 

        [self dismissViewControllerAnimated:YES completion:nil];

    }

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        NSLog(@"在相機中選擇圖片");

        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

        

        //設置圖片背景

        [userIconBtn setBackgroundImage:image forState:UIControlStateNormal];

        NSUserDefaults * user = [NSUserDefaults standardUserDefaults];

        NSData* imgData = UIImageJPEGRepresentation(image, 1);

        isPhotoChoose = YES;

        NSLog(@"第一次調用這個方法");

        [user setObject:imgData forKey:@"saveIcon"];

        [user synchronize];

        

        [self dismissViewControllerAnimated:YES completion:nil];

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM