Demo:直接上代码
#import "ViewController.h"
@interface ViewController ()<UIImagePickerControllerDelegate>
{
UIButton *_button;
}
@property (nonatomic, copy) NSString *imagePath;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/**
* 获取Documents文件夹路径
*
*
*/
// UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.view.backgroundColor = [UIColor redColor];
_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_button.frame = CGRectMake(100, 100, 100, 100);
[_button setTitle:@"添加照片" forState:UIControlStateNormal];
[_button addTarget:self action:@selector(addPic) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = path[0];
//指定新建文件夹路径
NSString *imageDocPath = [documentPath stringByAppendingPathComponent:@"ImageFile"];
//创建ImageFile文件夹
[[NSFileManager defaultManager] createDirectoryAtPath:imageDocPath withIntermediateDirectories:YES attributes:nil error:nil ];
//保存图片路径
self.imagePath = [imageDocPath stringByAppendingPathComponent:@"image.png"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
//根据图片路径载入图片
UIImage *image = [UIImage imageWithContentsOfFile:self.imagePath];
if (image == nil) {
[_button setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
}
else{
[_button setBackgroundImage:image forState:UIControlStateNormal];
}
}
- (void)addPic
{
NSString *cancelButtonTitle = NSLocalizedString(@"从相册选取", nil);
NSString *otherButtonTitle = NSLocalizedString(@"拍照", nil);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self LocalPhoto];
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self takePhoto];
}];
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//从资源类型为图片库
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后图片可以被编辑
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:^{
}];
}
- (void)takePhoto
{
//资源类型为照相机
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
//判断是否有相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:^{
}];
}
else
{
NSLog(@"该设备无摄像头");
}
}
#pragma mark -
#pragma mark Delegate method UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo
{
if (image != nil) {
//图片显示在界面上
[_button setBackgroundImage:image forState:UIControlStateNormal];
//保存文件到沙盒
//将图片转成NSData类型的数据来保存文件
NSData *data;
//判断图片是不是png格式的文件
if (UIImagePNGRepresentation(image)) {
//返回为png图像
data = UIImagePNGRepresentation(image);
}
else
{
//返回为JPEG图像
data = UIImageJPEGRepresentation(image, 1.0);
}
//保存
[[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];
}
//关闭相册界面
[picker dismissViewControllerAnimated:YES completion:^{
NSLog(@"sb");//关闭相册回到界面做的事情
}];
}
@end