添加单一原图 和添加单一可编辑图片
1.再点击事件中创建UIImagePickerController
- (IBAction)albumBtn2:(UIButton*)sender {
UIImagePickerController *ipc=[[UIImagePickerController alloc]init];
ipc.delegate=self;
[ipc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:ipc animated:YES completion:nil];
}
//每次添加一张图片 没选择图片执行一次
- (void)p_w_picpathPickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
//选取图片为原图
UIImage *p_w_picpath=info[UIImagePickerControllerOriginalImage];
UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(0, 50, 320, 100)];
iv.p_w_picpath=p_w_picpath;
[self.view addSubview:iv];
[self dismissViewControllerAnimated:YES completion:nil];
}
添加单一可编辑图片
- (IBAction)albumBtn1:(UIButton*)sender {
UIImagePickerController *ipc=[[UIImagePickerController alloc]init];
ipc.delegate=self;
//开启图片编辑 截图
ipc.allowsEditing=YES;
[ipc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:ipc animated:YES completion:nil];
}
//每次添加一张图片 没选择图片执行一次
- (void)p_w_picpathPickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
//开启图片编辑后 选取的图片为截图 截图没办法多选
UIImage *p_w_picpath=info[UIImagePickerControllerEditedImage];
UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(0, 50, 320, 100)];
iv.p_w_picpath=p_w_picpath;
[self.view addSubview:iv];
[self dismissViewControllerAnimated:YES completion:nil];
}
添加多张图片
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
- (IBAction)clicked:(id)sender;
@property (nonatomic, strong)UIScrollView *sv;
@property (nonatomic, strong)NSMutableArray*selectedIVs;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.selectedIVs = [NSMutableArray array];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)clicked:(id)sender {
UIImagePickerController *ipc = [[UIImagePickerController alloc]init];
// 添加协议,按住command键点击delegate获取<UINavigationControllerDelegate, UIImagePickerControllerDelegate>协议
ipc.delegate = self;
// 设置图片来源
[ipc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// ipc.allowsEditing = YES;
[self presentViewController:ipc animated:YES completion:nil];
}
- (void)p_w_picpathPickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
NSLog(@"%@",info);
UIImage *p_w_picpath = info[UIImagePickerControllerOriginalImage];
UIImageView *iv = [[UIImageViewalloc]initWithFrame:CGRectMake(self.selectedIVs.count*80, 0, 80, 80)];
iv.p_w_picpath = p_w_picpath;
//添加删除按钮
UIButton *delBtn = [[UIButtonalloc]initWithFrame:CGRectMake(60, 0, 20, 20)];
[delBtn setTitle:@"X" forState:UIControlStateNormal];
[delBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
[iv addSubview:delBtn];
iv.userInteractionEnabled = YES;
[self.sv addSubview:iv];
[self.selectedIVsaddObject:iv];
[self.sv setContentSize:CGSizeMake(self.selectedIVs.count*80, 0)];
// [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)deleteAction:(UIButton*)btn{
UIImageView *iv = (UIImageView *)btn.superview;
[iv removeFromSuperview];
[self.selectedIVsremoveObject:iv];
for (int i=0; i<self.selectedIVs.count; i++) {
UIImageView *iv = self.selectedIVs[i];
[UIView animateWithDuration:1 animations:^{
iv.frame = CGRectMake(i*80,0, 80, 80);
iv.alpha = 0;
}];
//哪些改变可以有动画
// center frame transform alpha backgroundColor
}
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController*)viewController animated:(BOOL)animated{
//判断出第二个页面
if (navigationController.viewControllers.count==2) {
UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 468, 320, 100)];
v.backgroundColor = [UIColor redColor];
[viewController.view addSubview:v];
self.sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 320, 80)];
self.sv.backgroundColor = [UIColor blueColor];
[v addSubview:self.sv];
}
}
转载于:https://blog.51cto.com/10685945/1702281