IOS 开发之操作图库自定义控制器
步骤如下:
新建此类的代理属性必须遵守的协议:
新建PhotoButtonDelegate.h如下:
//
// PhotoButtonDelegate.h
// 作业整理
//
// Created by apple on 15/9/16.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <Foundation/Foundation.h>
@class ImageAndPhotos;
@protocol PhotoButtonDelegate <NSObject>
-(void) setPhotoButton:(ImageAndPhotos *) imgAndP;
@end
新建此类如下:
编辑ImageAndPhotos.h如下:
//
// ImageAndPhotos.h
// 作业整理
//
// Created by apple on 15/9/16.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "PhotoButtonDelegate.h"
@class UIBaseScrollView;
@interface ImageAndPhotos : NSObject <UIAlertViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) UIViewController *controller;
@property (nonatomic, strong) UIImage *img;
@property (nonatomic, strong) UIButton *btn;
@property (nonatomic, weak) id<PhotoButtonDelegate> delegate;
-(id)initWithControler:(UIViewController *) crtler AndButton:(UIButton *) button;
@end
编辑ImageAndPhotos.m如下:
//
// ImageAndPhotos.m
// 作业整理
//
// Created by apple on 15/9/16.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ImageAndPhotos.h"
@implementation ImageAndPhotos
-(id)initWithControler:(UIViewController *) crtler AndButton:(UIButton *) button
{
if (self = [super init]) {
self.controller = crtler;
self.btn = button;
[self CameraEvent];
}
return self;
}
-(void)CameraEvent
{
[self.btn addTarget:self action:@selector(showActionSheet) forControlEvents:UIControlEventTouchUpInside];
}
-(void) showActionSheet
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"我的相册", nil nil];
[actionSheet showInView:self.controller.view];
}
// 实现UIActionSheetDelegate协议中监听按钮的方法
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
[self addCamera];
}
else if(buttonIndex == 1)
{
[self addPhoto];
}
}
-(void)addCamera
{
// 判断是否可以打开一个相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// 创建一个调出拍照的控制器
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
// 摄像头
NSLog(@"++++addCamera++++");
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.controller presentViewController:picker animated:YES completion:^{
}];
}
else
{
[self showAlertView];
}
}
-(void) addPhoto
{ // 相册可以用模拟器打开,但是相机不可以用模拟器打开
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES; // 是否可以编辑
// 打开相册选择相片
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //表示管理图库
[self.controller presentViewController:picker animated:YES completion:nil];
}
else
{
[self showAlertView];
}
}
-(void)showAlertView
{
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"提示" message:@"你没有摄像头" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];
[alert show];
}
// 代理协议中的方法
// 拍摄完成后,其实是选中图片后的方法要执行的方法,如果是照相的话则选中拍照后的相片
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// 得到图片
self.img = [info objectForKey:UIImagePickerControllerEditedImage];
// 图片存入图库
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(self.img, nil, nil, nil); // 如果是相机
}
[self.controller dismissViewControllerAnimated:YES completion:^{
if ([self.delegate respondsToSelector:@selector(setPhotoButton:)]) {
[self.delegate setPhotoButton:self];
}
}];
}
//选中图片点击cancel按钮后执行的方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self.controller dismissViewControllerAnimated:YES completion:nil];
}
@end










