zoukankan      html  css  js  c++  java
  • 非定制UIImagePickerController的使用

    非定制UIImagePickerController的使用

    效果:

    源码:

    //
    //  ViewController.m
    //  ImagePic
    //
    //  Created by XianMingYou on 15/3/26.
    //  Copyright (c) 2015年 XianMingYou. All rights reserved.
    //
    
    #import "ViewController.h"
    
    typedef enum : NSUInteger {
        TAKE_IMAGE,
        TAKE_PHOTO,
    } EChooseFlag;
    
    @interface ViewController ()<UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
    @property (nonatomic, strong) UIButton    *button;
    @property (nonatomic, strong) UIImageView *showImageView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self.view addSubview:self.showImageView];
        [self.view addSubview:self.button];
    }
    
    // 照片
    @synthesize showImageView = _showImageView;
    - (UIImageView *)showImageView {
        if (_showImageView == nil) {
            _showImageView                     = [[UIImageView alloc] initWithFrame:self.view.bounds];
            _showImageView.layer.masksToBounds = YES;
            _showImageView.contentMode         = UIViewContentModeScaleAspectFill;
        }
        
        return _showImageView;
    }
    
    // 按钮
    @synthesize button = _button;
    - (UIButton *)button {
        if (_button == nil) {
            
            CGRect  rect   = self.view.bounds;
            CGFloat height = rect.size.height;
            CGFloat width  = rect.size.width;
            
            _button = [[UIButton alloc] initWithFrame:CGRectMake(0, height - 60, width, 60)];
            [_button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
            [_button setTitle:@"Take" forState:UIControlStateNormal];
            [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            _button.backgroundColor = [UIColor blackColor];
        }
        
        return _button;
    }
    
    - (void)buttonEvent:(id)sender {
        [self initActionSheet];
    }
    
    // ActionSheet
    - (void)initActionSheet {
        UIActionSheet *pickerActionSheet = 
        [[UIActionSheet alloc] initWithTitle:@"选择"
                                    delegate:self
                           cancelButtonTitle:@"取消"
                      destructiveButtonTitle:nil
                           otherButtonTitles:@"获取系统相册", @"拍照", nil];
        [pickerActionSheet showInView:self.view];
    }
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex == TAKE_IMAGE) {
            [self takeImage];
        } else if (buttonIndex == TAKE_PHOTO) {
            [self takePhoto];
        } else {
    
        }
    }
    
    // 获取图片控制器
    - (void)takeImage {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.view.backgroundColor     = [UIColor whiteColor];
        imagePicker.delegate                 = self;
        imagePicker.sourceType               = UIImagePickerControllerSourceTypePhotoLibrary;
    
        /*
            UIImagePickerControllerSourceTypePhotoLibrary       文件夹管理形式
            UIImagePickerControllerSourceTypeSavedPhotosAlbum   显示所有文件形式
        */
        
        [self presentController:imagePicker];
    }
    
    // 获取图片控制器
    - (void)takePhoto {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.view.backgroundColor     = [UIColor whiteColor];
        imagePicker.delegate                 = self;
        imagePicker.sourceType               = UIImagePickerControllerSourceTypeCamera;
        
        [self presentController:imagePicker];
    }
    
    
    // 图片控制器代理
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        UIImage *image = info[UIImagePickerControllerOriginalImage];
        if (image) {
            self.showImageView.image = image;
            [self dismissController:picker];
        }
    }
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
        [self dismissController:picker];
    }
    
    
    
    - (void)dismissController:(UIViewController *)controller {
        [controller dismissViewControllerAnimated:YES completion:^{}];
    }
    - (void)presentController:(UIViewController *)controller {
        [self presentViewController:controller animated:YES completion:^{}];
    }
    
    
    
    @end

    一些小细节:

    实现两个代理方法:

  • 相关阅读:
    Scala 学习 (八) 高级语法
    Scala 学习 (七) 并发编程模型Akka
    Scala 学习 (六) 面向对象
    Scala 学习(五) Set和HashSet
    Scala 学习(四) 集合之List
    Scala 学习(三) 集合之数组
    Scala 学习(二) 方法和函数
    datatable动态添加,及填充数据
    因为数据库正在使用,所以无法获得对数据库的独占访问权
    win2003超过最大连接数
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4369352.html
Copyright © 2011-2022 走看看