IOS 开发之操作图库自定义控制器

所属分类: 软件编程 / IOS 阅读数: 1166
收藏 0 赞 0 分享

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 

此类新建完成,在自定义控件中的应用如下:(此自定义控件是一个上传图片的scrollVIew)

新建自定义控件类编辑UIBaseScrollView.h如下

// 
// UIBaseScrollView.h 
// 作业整理 
// 
// Created by apple on 15/9/16. 
// Copyright (c) 2015年 LiuXun. All rights reserved. 
// 
 
#import "UIBaseVIew.h" 
#import "ImageAndPhotos.h" 
 
 
@interface UIBaseScrollView : UIBaseVIew<PhotoButtonDelegate> 
 
@property (nonatomic, strong) NSMutableArray *arrayImgs; 
@property (nonatomic, strong) UIScrollView *scroll; 
@property (nonatomic, strong) ImageAndPhotos *imgChange; 
@property (nonatomic, strong) UIButton *btnImg; 
@property (nonatomic, strong) UIImageView *imgV; 
-(id)initWithFrame:(CGRect)frame CurrenContr:(UIViewController *) crtl; 
 
@end 
编辑定义控件的.m文件如下:

[objc] view plain copy
// 
// UIBaseScrollView.m 
// 作业整理 
// 
// Created by apple on 15/9/16. 
// Copyright (c) 2015年 LiuXun. All rights reserved. 
// 
 
#import "UIBaseScrollView.h" 
 
@implementation UIBaseScrollView 
 
-(id)initWithFrame:(CGRect)frame CurrenContr:(UIViewController *) crtl 
{ 
  if (self = [super initWithFrame:frame]) { 
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
     
    self.btnImg = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, frame.size.height-20, frame.size.height-20)]; 
    [self.btnImg setImage:[UIImage imageNamed:@"tizhong_photo_increase_bj"] forState:UIControlStateNormal]; 
     
    self.imgChange = [[ImageAndPhotos alloc] initWithControler:crtl AndButton:self.btnImg]; 
    self.scroll.showsHorizontalScrollIndicator = YES; 
    self.imgChange.delegate = self; 
    [self.scroll addSubview:self.btnImg]; 
    [self addSubview:self.scroll]; 
  } 
  return self; 
} 
 
-(void)setPhotoButton:(ImageAndPhotos *)imgAndP 
{ 
  NSLog(@"%@&&&&&&&&&",self.imgChange.img); 
  if (imgAndP.img) { 
    self.imgV =[[UIImageView alloc] initWithFrame: self.btnImg.frame ]; 
    self.imgV.image = imgAndP.img; 
    self.imgV.backgroundColor = [UIColor yellowColor]; 
    [self.scroll addSubview:self.imgV]; 
    self.btnImg.frame = CGRectMake(CGRectGetMaxX(self.imgV.frame)+10, self.imgV.frame.origin.y, self.imgV.frame.size.width, self.imgV.frame.size.height); 
    self.scroll.contentSize = CGSizeMake(CGRectGetMaxX(imgAndP.btn.frame)+10, 0); 
    if (CGRectGetMaxX(self.btnImg.frame)>self.scroll.frame.size.width) { 
      self.scroll.contentOffset = CGPointMake(self.btnImg.frame.origin.x-10, 0); 
    } 
  } 
 
} 
 
@end 

在控制器中使用此自定义控件如下:

UIBaseScrollView *det5 = [[UIBaseScrollView alloc] initWithFrame:CGRectMake
(20, CGRectGetMaxY(det4.frame)+20, WIDTH-40, 80) CurrenContr:self]; 

运行结果如下:


在控制器中直接使用此相册类也与此类似,不同之处就是让所在控制器遵守类属性的协议,然后实现即可,在此不再奥数。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

详细整理iOS中UITableView的性能优化

最近在微博上看到一个很好的开源项目,是关于如何优化UITableView的,加上正好最近也在优化项目中的类似朋友圈功能这块,思考了很多关于UITableView的优化技巧,所以决定详细的整理下对优化UITableView的理解,需要的朋友们可以参考借鉴。
收藏 0 赞 0 分享

IOS开发中禁止NavigationController的向右滑动返回

这篇文章主要介绍了IOS开发中禁止NavigationController的向右滑动返回的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现微信/QQ显示最近拍摄图片的功能实例代码

如果你刚刚拍摄了图片,在使用微信/QQ发生消息时会显示“你可能要发送的图片”,这个功能非常人性化,怎么实现的呢?下面小编给大家分享iOS实现微信/QQ显示最近拍摄图片的功能实例代码,一起看看吧
收藏 0 赞 0 分享

iOS实现动态自适应标签

这篇文章主要为大家详细介绍了iOS动态自适应标签的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS实现图片存在本地、再从本地获取图片的功能

本文主要介绍了iOS实现图片存在本地、再从本地获取图片的功能的代码。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS视频录制(或选择)压缩及上传功能(整理)

最新做的一个功能涉及到了视频的录制、压缩及上传功能,经过大神的一番教导,终于倒腾清楚了,今天小编把问题经过记录一下分享到脚本之家平台,供大家参考
收藏 0 赞 0 分享

iOS中打包上传常见的错误与解决办法

关于打包上传至AppStore,大家都认为是最后一步了,其实到了这里往往会遇到很多的坑。对于踩过的坑我不想再踩第二遍,所以在此将我遇到的所有奇葩问题在此做一个记录,当作对自己的一个提醒,同时也分享给给位,需要的朋友可以参考下。
收藏 0 赞 0 分享

解决Xcode 8构建版本iTunes Connect获取不到应用程序状态的办法

这篇文章主要介绍了关于解决Xcode 8构建版本iTunes Connect获取不到应用程序状态的办法,需要的朋友可以参考下
收藏 0 赞 0 分享

Objective-C实现身份证验证的方法示例

这篇文章主要给大家分享了Objective-C实现身份证验证的方法,文中给出了详细的示例代码,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

ios启动页强制竖屏(进入App后允许横屏与竖屏)

最近工作遇到这样一个需要,当进入启动页需要强制竖屏,而进入APP后就允许横屏与竖屏,通过查找相关的资料找到了解决的方法,所以将实现的方法整理后分享出来,需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享
查看更多