从相册获取图片进行编辑
1、搭建界面,添加按钮进行关联
2、从点击按钮跳转到相册的界面
3、选择将要跳转下一页面
4、已经完成选择图片
@property (nonatomic, strong)UIScrollView *sv;
@property (nonatomic, strong)NSMutableArray *seletedIVs;
@end
@implementation ViewController
- (IBAction)clicked:(id)sender {
self.seletedIVs = [NSMutableArray array];
UIImagePickerController *ipc = [[UIImagePickerController alloc]init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.delegate = self;
//是否允许编辑
// ipc.allowsEditing = YES;
[self presentViewController:ipc animated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@"%@",UIImagePickerControllerOriginalImage);
NSLog(@"%@",@"UIImagePickerControllerOriginalImage");
UIImage *image = info[UIImagePickerControllerOriginalImage];
// 通过数组计数 让图片的x轴和数组的数量建立关系
UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(self.seletedIVs.count*80, 0, 80, 80)];
iv.image = image;
[self.sv addSubview:iv];
//打开交互
iv.userInteractionEnabled = YES;
// 往图片中添加删除按钮
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(60, 0, 20, 20)];
[btn setTitle:@"X" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
[iv addSubview:btn];
[self.seletedIVs addObject:iv];
[self.sv setContentSize:CGSizeMake(self.seletedIVs.count*80, 0)];
// [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)deleteAction:(UIButton *)btn{
//拿到按钮所在的图片
UIImageView *iv = (UIImageView *)btn.superview;
//从数组中删除
[self.seletedIVs removeObject:iv];
// 从界面中删除
[iv removeFromSuperview];
for (int i=0; i<self.seletedIVs.count; i++) {
UIImageView *iv = self.seletedIVs[i];
[UIView animateWithDuration:.5 animations:^{
iv.frame = CGRectMake(i*80, 0, 80, 80);
}];
}
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (navigationController.viewControllers.count==2) {
UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 567, 375, 100)];
v.backgroundColor = [UIColor redColor];
[viewController.view addSubview:v];
self.sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 375, 80)];
self.sv.backgroundColor = [UIColor blueColor];
[v addSubview:self.sv];
//添加返回按钮
UIButton *doneBtn = [[UIButton alloc]initWithFrame:CGRectMake(295, 0, 80, 20)];
[doneBtn setTitle:@"Done" forState:UIControlStateNormal];
[doneBtn addTarget:self action:@selector(finishAction:) forControlEvents:UIControlEventTouchUpInside];
[v addSubview:doneBtn];
}
}
-(void)finishAction:(UIButton*)btn{
[self dismissViewControllerAnimated:YES completion:nil];
}