iOS中選擇相簿照片新增到應用程式中
阿新 • • 發佈:2019-02-17
例如QQ、微信等的說說,評論等功能需要呼叫手機相簿新增並發表圖片。
再此就簡單的介紹一下如何從相簿中新增圖片。
1、首先建立呼叫相簿的按鈕,再次是給UIImageView新增點選手勢來實現。
self.tupianImageview = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-69, 50, 20, 20)];
self.tupianImageview.image = [UIImage imageNamed:@"muzhi_tupian_1"];
self.tupianImageview.tag = 21 ;
[self.view addSubview:self.tupianImageview];
self.tupianImageview.userInteractionEnabled = YES;
UITapGestureRecognizer * PicTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addPic:)];
PicTap.delegate = self;
[self.tupianImageview addGestureRecognizer:PicTap];
2、實現點選手勢的響應方法
這裡新增一個提示框,提示選擇是拍照(目前沒有做,提示框中不顯示)還是從相簿選擇照片。
-(void)addPic:(UITapGestureRecognizer *)tap{
if (IOS8) {
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"獲取圖片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction * defaultActionPic = [UIAlertAction actionWithTitle:@"相簿" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
ZZPhotoController *photoController = [[ZZPhotoController alloc]init];
photoController.selectPhotoOfMax = 4;
[photoController showIn:self result:^(id responseObject){
NSArray *array = (NSArray *)responseObject;
// NSLog(@"-------------------%@",responseObject);
// [self.imagePickerArray addObjectsFromArray:array];
self.imagePickerArray = [NSMutableArray arrayWithArray:array];
[self addbottomImage];
}];
}];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIAlertAction * defaultActionCamer = [UIAlertAction actionWithTitle:@"相機" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
ZZCameraController *cameraController = [[ZZCameraController alloc]init];
cameraController.takePhotoOfMax = 4;
cameraController.isSaveLocal = NO;
[cameraController showIn:self result:^(id responseObject){
// NSLog(@"%@",responseObject);
NSArray *array = (NSArray *)responseObject;
[self.imagePickerArray addObjectsFromArray:array];
[self addbottomImage];
}];
}];
[alert addAction:defaultActionCamer];
}
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:cancelAction];
[alert addAction:defaultActionPic];
[self presentViewController:alert animated:YES completion:nil];
}
else{
UIActionSheet * sheet;
//判斷是否支援相機
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sheet =[ [UIActionSheet alloc]initWithTitle:@"獲取圖片" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"相機",@"相簿", nil];
}else{
sheet = [[UIActionSheet alloc]initWithTitle:@"獲取圖片" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"相簿", nil];
}
[sheet showInView:self.view];
}
}
判斷的是iOS系統版本
#define IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ? YES : NO)
3、實現選擇方法
#import <Foundation/Foundation.h>
#import "Common.h"
typedef void(^ZZCameraResult)(id responseObject);
@interface ZZCameraController : NSObject
@property (assign, nonatomic) BOOL isSaveLocal;
/*
* 設定最多連拍張數
*/
@property (assign, nonatomic) NSInteger takePhotoOfMax;
/*
* 設定圖片返回型別
*/
@property (assign, nonatomic) ZZImageType imageType;
-(void)showIn:(UIViewController *)controller result:(ZZCameraResult)result;
@end
#import "ZZCameraController.h"
#import "ZZCameraPickerViewController.h"
@interface ZZCameraController()
@property (strong,nonatomic) ZZCameraPickerViewController *cameraPickerController;
@end
@implementation ZZCameraController
-(ZZCameraPickerViewController *)cameraPickerController
{
if (!_cameraPickerController) {
_cameraPickerController = [[ZZCameraPickerViewController alloc]init];
}
return _cameraPickerController;
}
-(void)showIn:(UIViewController *)controller result:(ZZCameraResult)result
{
self.cameraPickerController.CameraResult = result;
//設定連拍最大張數
self.cameraPickerController.takePhotoOfMax = self.takePhotoOfMax;
//設定返回圖片型別
self.cameraPickerController.imageType = self.imageType;
self.cameraPickerController.isSavelocal = self.isSaveLocal;
[controller presentViewController:self.cameraPickerController animated:YES completion:nil];
}
@end
拍照的後續會發布。