在平时使用手机的时候,有时会需要一张图片(比如设置头像的时候),这时我们就需要选择相机拍照,还是在相册中选择,下面就介绍这个方法
在 .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];
}