iOS8之后UIAlertView被弃用 UIAlertController成功上位

    xiaoxiao2025-10-24  10

    如图所示当你UIAlert时  UIAlertView被划了一道红线  说明oc已经弃用了UIAlertView  随之而来的是UIAlertController

    那么UIAlertController怎么用呢  会不会很麻烦

    别急  看代码

    我现在需要一个弹出框  然后选择图片的来源 是从相册获取还是拍照

    代码  点击已经设置好的icon  弹框  

    #pragma mark --更改icon-- - (void)changeIconImage{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择图片来源" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *photography = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self photography]; }]; UIAlertAction *photoSelect = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self photoSelect]; }]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:photography]; [alert addAction:photoSelect]; [alert addAction:cancel]; [self presentViewController:alert animated:YES completion:nil]; // UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@"选择图片来源" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选取", @"拍照", nil]; // // [as showInView:self.view]; }

    #pragma mark --photography拍照-- - (void)photography{ UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; ipc.navigationBar.backgroundColor = [UIColor redColor]; //判断设备是否支持这种sourcetype if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ ipc.sourceType = UIImagePickerControllerSourceTypeCamera; } ipc.delegate = self; //是否允许编辑 ipc.allowsEditing = YES; [self presentViewController:ipc animated:YES completion:nil]; } - (void)photoSelect{ UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; ipc.navigationBar.backgroundColor = [UIColor redColor]; //判断设备是否支持这种sourcetype if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){ ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } ipc.delegate = self; //是否允许编辑 ipc.allowsEditing = YES; [self presentViewController:ipc animated:YES completion:nil]; } #pragma mark---ImagePickerControllerDelegate--- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; iconImageView.image = image; [picker dismissViewControllerAnimated:NO completion:nil]; } 这三段代码连起来问题就解决了 不过亲 要记得加上  UIImagePickerControllerDelegate , UINavigationControllerDelegate 这两个 下面解决问题的图片  测试 图片成功替换  没任何问题

    转载请注明原文地址: https://ju.6miu.com/read-1303485.html
    最新回复(0)