https://github.com/YouXianMing

非定制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

一些小细节:

实现两个代理方法:

 

posted @ 2015-03-26 20:42  YouXianMing  阅读(348)  评论(0编辑  收藏  举报