全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

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  # 操作图库自定义控制器  # 操作图库  # IOS 开发状态栏隐藏的实现办法  # iOS适配https证书问题(AFNetworking3.0为例)  # iOS开发-调用系统相机和相册获取照片示例  # iOS利用AFNetworking实现文件上传的示例代码  # ios常见加密解密方法(RSA、DES 、AES、MD5)  # iOS 使用 socket 实现即时通信示例(非第三方库)  # IOS 使用Block二次封装AFNetworking 3.0详解  # iOS实现录音转码MP3及转码BASE64上传示例  # 自定义  # 此类  # 可以用  # 是一个  # 器中  # 在此  # 希望能  # 与此  # 谢谢大家  # 创建一个  # 上传图片  # 不同之处  # 判断是否  # 奥数  # 完成后  # 类属  # init  # implementation  # super  # CameraEvent 


相关文章: 免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?  常州自助建站:操作简便模板丰富,企业个人快速搭建网站  测试制作网站有哪些,测试性取向的权威测试或者网站?  高防服务器租用如何选择配置与防御等级?  网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?  开封网站制作公司,网络用语开封是什么意思?  如何确保西部建站助手FTP传输的安全性?  网站视频怎么制作,哪个网站可以免费收看好莱坞经典大片?  Swift开发中switch语句值绑定模式  常州自助建站费用包含哪些项目?  网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?  微信h5制作网站有哪些,免费微信H5页面制作工具?  高端智能建站公司优选:品牌定制与SEO优化一站式服务  如何通过建站之星自助学习解决操作问题?  网站插件制作软件免费下载,网页视频怎么下到本地插件?  c++如何打印函数堆栈信息_c++ backtrace函数与符号名解析【方法】  javascript中对象的定义、使用以及对象和原型链操作小结  存储型VPS适合搭建中小型网站吗?  较简单的网站制作软件有哪些,手机版网页制作用什么软件?  无锡营销型网站制作公司,无锡网选车牌流程?  免费网站制作模板下载,除了易企秀之外还有什么H5平台可以制作H5长页面,最好是免费的?  北京网站制作的公司有哪些,北京白云观官方网站?  青岛网站设计制作公司,查询青岛招聘信息的网站有哪些?  招商网站制作流程,网站招商广告语?  c++ stringstream用法详解_c++字符串与数字转换利器  高防网站服务器:DDoS防御与BGP线路的AI智能防护方案  建站主机是否属于云主机类型?  如何实现建站之星域名转发设置?  如何在阿里云通过域名搭建网站?  网站制作和推广的区别,想自己建立一个网站做推广,有什么快捷方法马上做好一个网站?  重庆市网站制作公司,重庆招聘网站哪个好?  宝盒自助建站智能生成技巧:SEO优化与关键词设置指南  如何在Windows 2008云服务器安全搭建网站?  如何通过NAT技术实现内网高效建站?  如何快速生成ASP一键建站模板并优化安全性?  微信网站制作公司有哪些,民生银行办理公司开户怎么在微信网页上查询进度?  宝塔建站无法访问?如何排查配置与端口问题?  黑客入侵网站服务器的常见手法有哪些?  如何在建站之星绑定自定义域名?  建站10G流量真的够用吗?如何应对访问高峰?  广州网站制作的公司,现在专门做网站的公司有没有哪几家是比较好的,性价比高,模板也多的?  网站app免费制作软件,能免费看各大网站视频的手机app?  logo在线制作免费网站在线制作好吗,DW网页制作时,如何在网页标题前加上logo?  如何快速搭建高效香港服务器网站?  如何快速配置高效服务器建站软件?  深入理解Android中的xmlns:tools属性  湖南网站制作公司,湖南上善若水科技有限公司做什么的?  如何选择香港主机高效搭建外贸独立站?  建站主机如何安装配置?新手必看操作指南  网站制作中优化长尾关键字挖掘的技巧,建一个视频网站需要多少钱? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。