当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS 开发之操作图库自定义控制器

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

欲医神针,创业加盟网站大全,包皮过长治疗

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]; 

运行结果如下:


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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网