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

    一些小细节:

    实现两个代理方法:

  • 相关阅读:
    hdu 4027 Can you answer these queries? 线段树
    ZOJ1610 Count the Colors 线段树
    poj 2528 Mayor's posters 离散化 线段树
    hdu 1599 find the mincost route floyd求最小环
    POJ 2686 Traveling by Stagecoach 状压DP
    POJ 1990 MooFest 树状数组
    POJ 2955 Brackets 区间DP
    lightoj 1422 Halloween Costumes 区间DP
    模板 有源汇上下界最小流 loj117
    模板 有源汇上下界最大流 loj116
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4369352.html
Copyright © 2011-2022 走看看