iOS-iOS8之后的UIAlertViewController

一、iOS8之前
1、使用UIActionSheet作为弹出框
                UIActionSheet *actionSheet;
                //判断是否支持相机
                if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                    actionSheet = [[UIActionSheetalloc]initWithTitle:@"获取图片"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照",@"从相册选择", nil];
                }else{
                    actionSheet = [[UIActionSheetalloc]initWithTitle:@"获取图片"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"从相册选择",nil];
            }
               [actionSheet showInView:self.view];

#pragma mark- actionSheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (actionSheet.tag == 1000) {
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:
                    //来源:相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    //来源:相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                case 2:
                    return;
            }
        }
        else {
            if (buttonIndex == 2) {
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        // 跳转到相机或相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = sourceType;
        [self presentViewController:imagePickerController animated:YES completion:^{
        }];
    }
}

2、使用UIAlertView
-(void)showAlert:(NSString *)msg {  
        UIAlertView *alert = [[UIAlertView alloc]  
                              initWithTitle:@"Action Sheet选择项"  
                              message:msg  
                              delegate:self  
                              cancelButtonTitle:@"确定"  
                              otherButtonTitles: nil];  
        [alert show];  

二、iOS8之后将UIAlertView,UIActionSheet都使用UIAlertViewController来弹出提示框
#define IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0 ? YES : NO)
        if (IOS8)
            {
                UIAlertController *alertVC = [UIAlertControlleralertControllerWithTitle:@"获取图片"
                                                                                message:nil
                                                                         preferredStyle:UIAlertControllerStyleActionSheet];

                //判断是否支持相机(模拟器没有相机)
                BOOL isCamera = [UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
                if (isCamera)
                {
                    UIAlertAction *cameraAction = [UIAlertActionactionWithTitle:@"拍照"
                                                                           style:UIAlertActionStyleDefault
                                                                         handler:^(UIAlertAction *_Nonnull action) {
                        //相机
                        UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc]init];
                        imagePicker.delegate =self;
                        imagePicker.allowsEditing =YES;
                        imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;
                        [selfpresentViewController:imagePickeranimated:YEScompletion:nil];
                    }];
                    [alertVC addAction:cameraAction];
                }

                UIAlertAction *photoAction = [UIAlertActionactionWithTitle:@"从相册选择"
                                                                      style:UIAlertActionStyleDefault
                                                                    handler:^(UIAlertAction *_Nonnull action) {
                                                                        //相册
                                                                        UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc]init];
                                                                         imagePicker.delegate =self;
                                                                         imagePicker.allowsEditing =YES;
                                                                         imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
                                                                         [selfpresentViewController:imagePickeranimated:YEScompletion:nil];
                                                                     }];
            [alertVC addAction:photoAction];
            
            UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"
                                                                 style:UIAlertActionStyleCancel
                                                               handler:^(UIAlertAction *_Nonnull action) {
                                                                }];
            [alertVC addAction:cancelAction];
                
            //弹出视图 使用UIViewController方法
            [selfpresentViewController:alertVCanimated:YEScompletion:nil];
           }
            else
            {
                UIActionSheet *actionSheet;
                //判断是否支持相机
                if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                    actionSheet = [[UIActionSheetalloc]initWithTitle:@"获取图片"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照",@"从相册选择", nil];
                }else{
                    actionSheet = [[UIActionSheetalloc]initWithTitle:@"获取图片"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"从相册选择",nil];
            }
               [actionSheet showInView:self.view];
            }

#pragma mark- imagePicker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{ 
    }];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //保存图片至本地,上传图片到服务器需要使用
    [self saveImage:image withName:@"haha.png"];
    NSString *filePath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"haha.png"];
    UIImage *savedImage = [[UIImage alloc]initWithContentsOfFile:filePath];
    //设置图片显示
    [self.photoImgView setImage:savedImage];
}

//将图片保存至沙盒
- (void)saveImage:(UIImage *)currentImage withName:(NSString *)imgName
{
    //1为不缩放保存,取值(0.0 - 1.0)
    NSData *imgData = UIImageJPEGRepresentation(currentImage, 1);
    //获取沙盒目录
    NSString *filePath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imgName];
    NSLog(@"沙盒路径:%@",NSHomeDirectory());
    //将图片写入文件
    [imgData writeToFile:filePath atomically:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

 

 

 

 

 

还可参考博主转载的iOS8中提示框的使用UIAlertController(UIAlertView)

 

你可能感兴趣的:(iOS-OC)