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

    本文转发自“宏创学院”

  • 相关阅读:
    小结css2与css3的区别
    javascript变量的作用域
    javascript面向对象
    小结php中几种网页跳转
    foreach
    post与get,这两人到底神马区别??
    typescript遍历Map
    dataTable.js参数
    showModal()和show()的区别
    javascript中location.protocol、location.hostname和location.port
  • 原文地址:https://www.cnblogs.com/garywong1949/p/5484068.html
Copyright © 2011-2022 走看看