這篇文章將為大家詳細講解有關(guān)IOS如何實現(xiàn)操作圖庫自定義控制器,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
步驟如下:
新建此類的代理屬性必須遵守的協(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:@"拍照",@"我的相冊", nil nil]; [actionSheet showInView:self.controller.view]; } // 實現(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 { // 判斷是否可以打開一個相機 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { // 創(chuàng)建一個調(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 { // 相冊可以用模擬器打開,但是相機不可以用模擬器打開 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]; } // 代理協(xié)議中的方法 // 拍攝完成后,其實是選中圖片后的方法要執(zhí)行的方法,如果是照相的話則選中拍照后的相片 -(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按鈕后執(zhí)行的方法 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self.controller dismissViewControllerAnimated:YES completion:nil]; } @end
此類新建完成,在自定義控件中的應(yīng)用如下:(此自定義控件是一個上傳圖片的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];
運行結(jié)果如下:
在控制器中直接使用此相冊類也與此類似,不同之處就是讓所在控制器遵守類屬性的協(xié)議,然后實現(xiàn)即可,在此不再奧數(shù)。
關(guān)于“IOS如何實現(xiàn)操作圖庫自定義控制器”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站sd-ha.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁題目:IOS如何實現(xiàn)操作圖庫自定義控制器-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://sd-ha.com/article0/jjjoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計、虛擬主機、域名注冊、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容