iOS中选择相机还是相册

在平时使用手机的时候,有时会需要一张图片(比如设置头像的时候),这时我们就需要选择相机拍照,还是在相册中选择,下面就介绍这个方法


在 .h 文件中

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
    
    //新建一个图片选择器(相机或相册),并且添加代理
    UIImagePickerController *myImagePicker;
}

@end


在 .m 文件中

#import "ViewController.h"

@interface ViewController (){
    
    //新建一个图片界面,用来显示我们选择的图片
    UIImageView *myImageView;
}



@end

@implementation ViewController{
    
    //无符号整型
    NSUInteger *sourceType;
    
}



- (void)viewDidLoad {
    [super viewDidLoad];

    //设置图片界面的位置和大小,并添加到界面上
    myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 300, self.view.frame.size.width-100, 300)];
    myImageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:myImageView];
    
    //新建一个Button,点击后选择相机还是相册
    UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 50, 200, 40)];
    myButton.backgroundColor = [UIColor grayColor];
    [myButton setTitle:@"添加图片" forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton];
    
 
}



//Button的方法
-(void)haha:(id)sender{
    
    [self tagmuxh:nil];
    
}



-(void)tagmuxh:(id)sender{
    
    //新建一个底部弹框(这里用的是旧办法)
    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.tag = 110;
    
    [sheet showInView:self.view];
    

}



-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (actionSheet.tag == 110) {
        sourceType = 0;
        
        //判断是否支持相机
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:
                    //取消
                    return;
                case 1:
                    //相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 2:
                    //相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                    
            }
        }else{
            
            if (buttonIndex == 0) {
                return;
            }else{
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                
            }
            
        }
        
        //跳转到相机或相册页面
        [self performSelector:@selector(showcamera:) withObject:nil afterDelay:0.3];

    }
 
}



-(void)showcamera:(id)sender{
    
    myImagePicker = [[UIImagePickerController alloc]init];
    
    myImagePicker.delegate = self;
    
    myImagePicker.allowsImageEditing = YES;
    
    myImagePicker.sourceType = sourceType;
    
    [self presentViewController:myImagePicker animated:YES completion:NULL];
 
}



-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    NSLog(@"info%@", info);
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    //给文件起名
    NSString *stringtag = @"nihao";
    
    //保存Image
    [self saveImage:image withName:stringtag];
    
    NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents" ] stringByAppendingPathComponent:stringtag];

    NSLog(@"%@", filePath);
    
    //显示imageView
    UIImage *saveImage = [[UIImage alloc]initWithContentsOfFile:filePath];
    
    myImageView.image = saveImage;
    

}



//保存图片到沙盒文件
-(void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName{
    
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    
    //获取沙盒目录
    NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];
    
    //将图片写入文件
    [imageData writeToFile:filePath atomically:NO];
    

}


转载于:https://my.oschina.net/LBBB/blog/670377

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值