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

一些小细节:

实现两个代理方法:

目录
相关文章
|
Web App开发 Windows
用XAML做网页!!—开篇
原文:用XAML做网页!!—开篇      这几日一直没发表新文章,一来是因为事比较多,二来就是我在研究使用XAML挑战传统HTML来做网页,这很可能是在全球的首次尝试,至少我从未找到任何可供参考的相关资料。
1036 0
|
11天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1230 5
|
10天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1213 87
|
10天前
|
云栖大会
阿里云云栖大会2025年9月24日开启,免费申请大会门票,速度领取~
2025云栖大会将于9月24-26日举行,官网免费预约畅享票,审核后短信通知,持证件入场
1796 13
|
20天前
|
人工智能 运维 安全
|
3天前
|
资源调度
除了nrm-pm,还有哪些工具可以管理多个包管理器的源?
除了nrm-pm,还有哪些工具可以管理多个包管理器的源?
234 127