English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Custom Controller of Photo Library in iOS Development

Custom Controller of Photo Library in iOS Development

The steps are as follows:

The protocol that must be followed when creating an agent of this type:

Create the PhotoButtonDelegate.h file as follows:

// 
// PhotoButtonDelegate.h 
// Work Organization 
// 
// Created by apple on 15/9/16. 
// Copyright (c) 2015Year LiuXun. All rights reserved. 
// 
#import <Foundation/Foundation.h> 
@class ImageAndPhotos; 
@protocol PhotoButtonDelegate <NSObject> 
-(void) setPhotoButton:(ImageAndPhotos *) imgAndP; 
@end 

Create this class as follows:

Edit ImageAndPhotos.h as follows:

// 
// ImageAndPhotos.h 
// Work Organization 
// 
// Created by apple on 15/9/16. 
// Copyright (c) 2015Year 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 

Edit ImageAndPhotos.m as follows:

// 
// ImageAndPhotos.m 
// Work Organization 
// 
// Created by apple on 15/9/16. 
// Copyright (c) 2015Year 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:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take Photo",@"My Photo Album", nil nil]; 
  [actionSheet showInView:self.controller.view]; 
 } 
// Implement the method to listen to buttons in the UIActionSheetDelegate protocol 
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
  if (buttonIndex == 0) { 
    [self addCamera]; 
  } 
  else if(buttonIndex == 1) 
  { 
    [self addPhoto]; 
  } 
} 
-(void)addCamera 
{ 
  // Determine whether a camera can be opened 
  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
    // Create a controller to call out to take a photo 
    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    picker.allowsEditing = YES; 
    // Camera 
    NSLog(@"++++addCamera++++"); 
    picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    [self.controller presentViewController:picker animated:YES completion:^{ 
    }] 
  } 
  else 
  { 
    [self showAlertView]; 
  } 
} 
-(void) addPhoto 
{   // The photo album can be opened with an emulator, but the camera cannot be opened with an emulator. 
  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { 
    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    picker.allowsEditing = YES; // Whether it can be edited 
    // Open the album to select a photo 
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //Means to manage the library 
    [self.controller presentViewController:picker animated:YES completion:nil]; 
  } 
  else 
  { 
    [self showAlertView]; 
  } 
} 
-(void)showAlertView 
{ 
  UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Prompt" message:@"You do not have a camera" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil nil]; 
  [alert show]; 
} 
// Method in the delegate protocol 
// After the shooting is completed, it is actually the method to be executed after the image is selected, if it is a camera, then it is the photo after taking a picture 
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
  // Get the image 
  self.img = [info objectForKey:UIImagePickerControllerEditedImage]; 
  // Image saved to library 
  if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { 
    UIImageWriteToSavedPhotosAlbum(self.img, nil, nil, nil); // If it is a camera 
  } 
  [self.controller dismissViewControllerAnimated:YES completion:^{ 
    if ([self.delegate respondsToSelector:@selector(setPhotoButton:)]) { 
      [self.delegate setPhotoButton:self]; 
    } 
  }] 
} 
//The method executed after clicking the cancel button of the selected image 
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
  [self.controller dismissViewControllerAnimated:YES completion:nil]; 
} 
@end 

This class is created, and its application in the custom control is as follows: (This custom control is a scroll view for uploading images)

Create a custom control class and edit UIBaseScrollView.h as follows:

// 
// UIBaseScrollView.h 
// Work Organization 
// 
// Created by apple on 15/9/16. 
// Copyright (c) 2015Year 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 
Edit the .m file of the control definition as follows:
[objc] view plain copy
// 
// UIBaseScrollView.m 
// Work Organization 
// 
// Created by apple on 15/9/16. 
// Copyright (c) 2015Year 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 

The usage of this custom control in the controller is as follows:

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

The running result is as follows:


It is similar to using this album class directly in the controller. The difference is that the controller should comply with the protocol of the class attributes, and then implement it. No more will be discussed here.

Thank you for reading and hope it can help everyone. Thank you for your support of this site!

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like