新建工程 编辑控制器的.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
运行结果如下: