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

    本文转发自“宏创学院”

  • 相关阅读:
    java中equals与hashCode的重写问题
    关于java的二维码的生成与解析
    bootstrap的时间控件使用(双日历)
    Mysql表,列,库的增删查改
    关于js重名方法的先后调用问题
    javascript的比较运算符
    javascript的运算符
    JavaScript的数据类型
    javascript的基本语法
    MAP集合
  • 原文地址:https://www.cnblogs.com/garywong1949/p/5484068.html
Copyright © 2011-2022 走看看