本文主要是介绍UIAlertController封装(包含iPad),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先需要支持iOS 8.0及其以上的版本
UIAlertController 有两种展示方式
1 就是列表类型的
UIAlertControllerStyleActionSheet
2 就是弹出框类型的
UIAlertControllerStyleAlert
代码
.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>typedef enum : NSUInteger {selectImageType,selectMovieType,selectImageAndMovieType,
} SelectImageMovieType;typedef void(^completionImageBlock)(id imageOrMovie);@interface ImagePickerManager : NSObject<UIImagePickerControllerDelegate,UINavigationControllerDelegate>+(ImagePickerManager *)shareManager;-(void)showImagePickFromController:(UIViewController *)vcsourceType:(UIImagePickerControllerSourceType)sourceTypeimageMovieSelectedType:(SelectImageMovieType)imageMovieSelectedTypecompletion:(completionImageBlock)completion;-(void)showImagePickFromController:(UIViewController *)vcisEditing:(BOOL)isEditingisFlash:(BOOL)isFlashcompletion:(completionImageBlock)completion;-(void)showImagePickFromController:(UIViewController *)vccompletion:(completionImageBlock)completion;-(void)showImagePickFromAllowsEditing:(BOOL)allowsEditingvideoQuality:(UIImagePickerControllerQualityType)videoQualitysourceType:(UIImagePickerControllerSourceType)sourceTypemodalPresentationStyle:(UIModalPresentationStyle)modalPresentationStylecontroller:(UIViewController *)vcisFlash:(BOOL)isFlashimageMovieSelectedType:(SelectImageMovieType)imageMovieSelectedTypecompletion:(completionImageBlock)completion;@end
.m
#import "ImagePickerManager.h"
#import <MobileCoreServices/MobileCoreServices.h>@implementation ImagePickerManager
{UIViewController *_vc;completionImageBlock _comBlock;
}+(ImagePickerManager *)shareManager{static ImagePickerManager *instance = nil;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{instance = [[ImagePickerManager alloc] init];});return instance;
}-(void)showImagePickFromController:(UIViewController *)vcsourceType:(UIImagePickerControllerSourceType)sourceTypeimageMovieSelectedType:(SelectImageMovieType)imageMovieSelectedTypecompletion:(completionImageBlock)completion{[self showImagePickFromAllowsEditing:NOvideoQuality:UIImagePickerControllerQualityTypeLowsourceType:sourceTypemodalPresentationStyle:UIModalPresentationFullScreencontroller:vcisFlash:YESimageMovieSelectedType:imageMovieSelectedTypecompletion:completion];
}-(void)showImagePickFromController:(UIViewController *)vcisEditing:(BOOL)isEditingisFlash:(BOOL)isFlashcompletion:(completionImageBlock)completion{[self showImagePickFromAllowsEditing:isEditingvideoQuality:UIImagePickerControllerQualityTypeLow sourceType:UIImagePickerControllerSourceTypePhotoLibrary modalPresentationStyle:UIModalPresentationFullScreencontroller:vcisFlash:isFlashimageMovieSelectedType:selectImageTypecompletion:completion];
}-(void)showImagePickFromController:(UIViewController *)vccompletion:(completionImageBlock)completion{[self showImagePickFromAllowsEditing:NOvideoQuality:UIImagePickerControllerQualityTypeLow sourceType:UIImagePickerControllerSourceTypePhotoLibrary modalPresentationStyle:UIModalPresentationFullScreencontroller:vcisFlash:YESimageMovieSelectedType:selectImageTypecompletion:completion];
}-(void)showImagePickFromAllowsEditing:(BOOL)allowsEditingvideoQuality:(UIImagePickerControllerQualityType)videoQualitysourceType:(UIImagePickerControllerSourceType)sourceTypemodalPresentationStyle:(UIModalPresentationStyle)modalPresentationStylecontroller:(UIViewController *)vcisFlash:(BOOL)isFlashimageMovieSelectedType:(SelectImageMovieType)imageMovieSelectedTypecompletion:(completionImageBlock)completion{UIImagePickerController *picker = [[UIImagePickerController alloc] init];picker.delegate = self;//可以编辑picker.allowsEditing = allowsEditing;//设置图片质量picker.videoQuality=videoQuality;picker.sourceType = sourceType;picker.modalPresentationStyle = modalPresentationStyle;NSArray *mediaTypes = nil;switch (imageMovieSelectedType) {case selectImageType:{mediaTypes = @[(NSString *)kUTTypeImage];}break;case selectMovieType:{mediaTypes = @[(NSString *)kUTTypeMovie];}break;case selectImageAndMovieType:{mediaTypes = @[(NSString *)kUTTypeImage,(NSString *)kUTTypeMovie];}break;default:break;}picker.mediaTypes = mediaTypes;if (isFlash&&sourceType==UIImagePickerControllerSourceTypeCamera) {picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;}_vc = vc;_comBlock = [completion copy];[_vc presentViewController:picker animated:YES completion:^{}];
}//点击Cancel按钮后执行方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{[_vc dismissViewControllerAnimated:YES completion:^{}];
}-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{//主线程等待0.5秒回到根视图dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(0.51 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{if (![_vc.presentedViewController isBeingDismissed]) {[_vc dismissViewControllerAnimated:YES completion:^{}];}});NSString *mediaType = info[UIImagePickerControllerMediaType];if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {UIImage *editedImage = info[UIImagePickerControllerEditedImage];UIImage *originalImage = info[UIImagePickerControllerOriginalImage];UIImage *needImage = nil;if (originalImage) {needImage = originalImage;}if (editedImage) {needImage = editedImage;}if (_comBlock) {_comBlock(needImage);}}else if([mediaType isEqualToString:(NSString *)kUTTypeMovie]){NSURL *videoURL = info[UIImagePickerControllerMediaURL];if (videoURL) {if (_comBlock) {_comBlock(videoURL);}}}
}@end
这篇关于UIAlertController封装(包含iPad)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!