最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IOS 開發(fā)之操作圖庫自定義控制器

 更新時(shí)間:2017年02月20日 09:27:29   作者:一蓑煙雨任平生AAA  
這篇文章主要介紹了IOS 開發(fā)之操作圖庫自定義控制器的相關(guān)資料,需要的朋友可以參考下

IOS 開發(fā)之操作圖庫自定義控制器

步驟如下:

新建此類的代理屬性必須遵守的協(xié)議:

新建PhotoButtonDelegate.h如下:

// 
// PhotoButtonDelegate.h 
// 作業(yè)整理 
// 
// 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 
// 作業(yè)整理 
// 
// 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 
// 作業(yè)整理 
// 
// 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:@"拍照",@"我的相冊(cè)", nil nil]; 
  [actionSheet showInView:self.controller.view]; 
 } 
 
// 實(shí)現(xiàn)UIActionSheetDelegate協(xié)議中監(jiān)聽按鈕的方法 
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
  if (buttonIndex == 0) { 
    [self addCamera]; 
  } 
  else if(buttonIndex == 1) 
  { 
    [self addPhoto]; 
  } 
   
} 
 
-(void)addCamera 
{ 
  // 判斷是否可以打開一個(gè)相機(jī) 
  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
    // 創(chuàng)建一個(gè)調(diào)出拍照的控制器 
    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 
{   // 相冊(cè)可以用模擬器打開,但是相機(jī)不可以用模擬器打開 
  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { 
    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     
    picker.delegate = self; 
    picker.allowsEditing = YES; // 是否可以編輯 
     
    // 打開相冊(cè)選擇相片 
    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]; 
} 
 
// 代理協(xié)議中的方法 
// 拍攝完成后,其實(shí)是選中圖片后的方法要執(zhí)行的方法,如果是照相的話則選中拍照后的相片 
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
  // 得到圖片 
  self.img = [info objectForKey:UIImagePickerControllerEditedImage]; 
  // 圖片存入圖庫 
  if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { 
    UIImageWriteToSavedPhotosAlbum(self.img, nil, nil, nil); // 如果是相機(jī) 
  } 
   
  [self.controller dismissViewControllerAnimated:YES completion:^{ 
    if ([self.delegate respondsToSelector:@selector(setPhotoButton:)]) { 
      [self.delegate setPhotoButton:self]; 
    } 
  }]; 
   
} 
 
//選中圖片點(diǎn)擊cancel按鈕后執(zhí)行的方法 
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
   
  [self.controller dismissViewControllerAnimated:YES completion:nil]; 
} 
 
 
@end 

此類新建完成,在自定義控件中的應(yīng)用如下:(此自定義控件是一個(gè)上傳圖片的scrollVIew)

新建自定義控件類編輯UIBaseScrollView.h如下

// 
// UIBaseScrollView.h 
// 作業(yè)整理 
// 
// 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 
// 作業(yè)整理 
// 
// 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]; 

運(yùn)行結(jié)果如下:


在控制器中直接使用此相冊(cè)類也與此類似,不同之處就是讓所在控制器遵守類屬性的協(xié)議,然后實(shí)現(xiàn)即可,在此不再奧數(shù)。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • fastlane自動(dòng)化打包iOS APP過程示例

    fastlane自動(dòng)化打包iOS APP過程示例

    這篇文章主要為大家介紹了fastlane自動(dòng)化打包iOS APP的過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • iOS動(dòng)態(tài)更換Icon的全過程記錄

    iOS動(dòng)態(tài)更換Icon的全過程記錄

    這篇文章主要給大家介紹了關(guān)于iOS動(dòng)態(tài)更換Icon的全過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 詳解iOS開發(fā)中UIPickerView控件的使用方法

    詳解iOS開發(fā)中UIPickerView控件的使用方法

    這篇文章主要介紹了詳解iOS開發(fā)中UIPickerView控件的使用方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-11-11
  • IOS開發(fā)中取消文本框輸入時(shí)的小鍵盤

    IOS開發(fā)中取消文本框輸入時(shí)的小鍵盤

    這篇文章主要介紹了IOS開發(fā)中取消文本框輸入時(shí)的小鍵盤,需要的朋友可以參考下
    2015-05-05
  • iOS實(shí)現(xiàn)無限滑動(dòng)效果

    iOS實(shí)現(xiàn)無限滑動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)無限滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • IOS關(guān)閉鍵盤的方法

    IOS關(guān)閉鍵盤的方法

    在iOS應(yīng)用開發(fā)中,有三類視圖對(duì)象會(huì)打開虛擬鍵盤,進(jìn)行輸入操作,但如何關(guān)閉虛擬鍵盤,卻沒有提供自動(dòng)化的方法。這個(gè)需要我們自己去實(shí)現(xiàn)。
    2015-05-05
  • iOS開發(fā)系列--詳細(xì)介紹數(shù)據(jù)存取

    iOS開發(fā)系列--詳細(xì)介紹數(shù)據(jù)存取

    本篇文章主要介紹了iOS開發(fā)系列--詳細(xì)介紹數(shù)據(jù)存取,詳細(xì)介紹了IOS數(shù)據(jù)的存儲(chǔ)問題,具有一定的參考價(jià)值,有興趣的同學(xué)可以了解一下。
    2016-11-11
  • iOS時(shí)間字符串格式化輸出技巧詳解

    iOS時(shí)間字符串格式化輸出技巧詳解

    本篇文章主要介紹了iOS時(shí)間格式化輸出技巧,可以將后臺(tái)返回的時(shí)間字符串轉(zhuǎn)換為指定的格式時(shí)間再顯示在UI上,有興趣的可以了解一下。
    2017-04-04
  • iOS微信第三方登錄實(shí)現(xiàn)

    iOS微信第三方登錄實(shí)現(xiàn)

    這篇文章主要介紹了iOS微信第三方登錄實(shí)現(xiàn)的全過程,一步一步告訴大家iOS微信實(shí)現(xiàn)第三方登錄的方法,感興趣的小伙伴們可以參考一下
    2016-01-01
  • 淺析Objective-C中分類Category的使用

    淺析Objective-C中分類Category的使用

    這篇文章主要介紹了淺析Objective-C中分類Category的使用,使用Category對(duì)類進(jìn)行擴(kuò)展可以訪問原始類的實(shí)例變量,需要的朋友可以參考下
    2016-03-03

最新評(píng)論

新和县| 阜南县| 星子县| 清原| 富源县| 兴城市| 崇州市| 文水县| 叶城县| 盖州市| 临湘市| 贵州省| 星座| 阿尔山市| 炉霍县| 伽师县| 岫岩| 大冶市| 东城区| 河东区| 南陵县| 星子县| 鹰潭市| 阿拉善左旗| 山阴县| 镇赉县| 娱乐| 乃东县| 师宗县| 安仁县| 怀宁县| 乌拉特前旗| 扎赉特旗| 历史| 黎平县| 铅山县| 泊头市| 南部县| 黑龙江省| 伊通| 华容县|