iOS通過UIAlertController彈出底部選擇框來調用相機或者相冊


  UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
            
        }];
        
        UIAlertAction *camera = [UIAlertAction actionWithTitle:@"打開相機" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            }
            [self presentViewController:imagePicker animated:YEScompletion:nil];
            
        }];
        
        UIAlertAction *picture = [UIAlertAction actionWithTitle:@"打開相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
                
            }
            pickerImage.delegate = self;
            pickerImage.allowsEditing = NO;
            
            [self presentViewController:pickerImage animated:YEScompletion:nil];
        }];
        [alertVc addAction:cancle];
        [alertVc addAction:camera];
        [alertVc addAction:picture];
        [self presentViewController:alertVc animated:YES completion:nil];


// 上面寫到Button的點擊事件方法里,下面直接復制就OK,記得遵守代理協議
UIImagePickerControllerDelegate, UINavigationControllerDelegate


// 選擇圖片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //當選擇的類型是圖片
    if ([type isEqualToString:@"public.image"])
    {
        //先把圖片轉成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSData *data;
        if (UIImagePNGRepresentation(image) ==nil)
        {
            data = UIImageJPEGRepresentation(image,1.0);
        }
        else
        {
            data = UIImagePNGRepresentation(image);
        }
        
        //圖片保存的路徑
        //這里將圖片放在沙盒的documents文件夾中
        NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        
        //文件管理器
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        //把剛剛圖片轉換的data對象拷貝至沙盒中並保存為image.png
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/userHeader.png"] contents:data attributes:nil];
        
        //關閉相冊界面
        [picker dismissViewControllerAnimated:YEScompletion:nil];
        
        //加在視圖中
        [self.mineView.headerButton setBackgroundImage:image forState:(UIControlStateNormal)];
        
    }
}
// 取消選取圖片
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YEScompletion:nil];
}

也可以按照需求對UIAlertAction進行個性化設置,比如說改變字體顏色,字體大小等等

        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {

        }];

        //修改按鈕文字顏色

        [cancle setValue:[UIColor yellowColor] forKey:@"titleTextColor"];

 

 更多個性化設置可以參考下面資料

按鈕的個性化設置

 

 

參考資料


免責聲明!

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



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