iphone开发之UIImagePickerController组件的预习————用于照相和打开图库

新建工程 编辑控制器的.h文件如下:

//
//  ViewController.h
//  多媒体A
//
//  Created by apple on 15/9/4.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>

@property(nonatomic, strong) UIImageView *cameraImageView;

@end
编辑控制器的.m文件如下:

//
//  ViewController.m
//  多媒体A
//
//  Created by apple on 15/9/4.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self  initCameraBtnAndCaneraImageView];
}
-(void)addPhoto
{
   // 相册可以用模拟器打开,但是相机不可以用模拟器打开
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        NSLog(@"addPhoto");
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES; // 是否可以编辑
        
        // 打开相册选择相片
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  //表示管理图库
        
        [self presentViewController:picker animated:YES completion:^{ }];
    }
    else
    {
        [self showAlertView];
    }
}

-(void)showActionSheet
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"我的相册", nil];
    [actionSheet showInView:self.view];

}

// 实现UIActionSheetDelegate协议中监听按钮的方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self addCamera];
    }
    else if(buttonIndex == 1)
    {
        [self  addPhoto];
    
    }

}

-(void) initCameraBtnAndCaneraImageView
{
   // 代码实现自定义按钮
    UIButton *cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cameraBtn.frame = CGRectMake(120, 150, 80, 120);
    
    // 设置按钮的响应事件
    [cameraBtn addTarget:self action:@selector(showActionSheet) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cameraBtn];
    
    // 设置图片显示框
    self.cameraImageView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 150, 80, 120)];
    self.cameraImageView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.cameraImageView];

}

//打开相机
-(void)addCamera
{
    // 判断是否可以打开相机,模拟器的此功能无法使用
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        // 创建一个调出拍照的控制器
        UIImagePickerController *picker = [[UIImagePickerController alloc ] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        // 摄像头
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self  presentViewController:picker animated:YES completion:^{ }];
        
    }
    else
    {
        [self showAlertView];
        
    }
    
}

// 拍摄完成后要执行的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
     // 得到图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    // 图片存入相册
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    self.cameraImageView.image = image;
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

// 点击cancel按钮后执行的方法
-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];

}


-(void)showAlertView
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你没有摄像头" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
   
}


@end
运行结果如下:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值