zoukankan      html  css  js  c++  java
  • IOS相册或者照相机图片的选择

    #import "ViewController.h"
    
    @interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    - (IBAction)chooseImage:(id)sender {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选取图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
            [self presentViewController:imagePickerController animated:YES completion:^{}];
        }];
        
        UIAlertAction *photosAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:imagePickerController animated:YES completion:^{}];
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            [alert addAction:cameraAction];
        }
        
        [alert addAction:photosAction];
        [alert addAction:cancelAction];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    #pragma mark - UIImagePickerControllerDelegate
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        
        [picker dismissViewControllerAnimated:YES completion:nil];
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        self.imageView.image = image;
        SEL selectorToCall = @selector(image:didFinishSavingWithError:contextInfo:);
    
        UIImageWriteToSavedPhotosAlbum(image, self, selectorToCall, NULL);
    }
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
        if (error == nil){
            NSLog(@"Image was saved successfully.");
        } else {
            NSLog(@"An error happened while saving the image.");
            NSLog(@"Error = %@", error);
        }
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end

    本文转发自“宏创学院”

  • 相关阅读:
    Ax+By+C=0 直线一般式拟合 c++/python
    win7结束进程 时,提示“拒绝访问”、“没有此任务的实例运行” taskkill 结束不了进程
    Keras神经网络转到Android可用的模型
    Keras深度神经网络训练IMDB情感分类的四种方法
    解决win7 word 2016中不能加载EndNote x7
    Google Coral Dev Board
    pydot` failed to call GraphViz.Please install GraphViz
    使用可视化图表对 Webpack 2 的编译与打包进行统计分析
    webpack 多环境配置
    格式化时间
  • 原文地址:https://www.cnblogs.com/garywong1949/p/5484068.html
Copyright © 2011-2022 走看看